Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import function from .pyx file in python?

Tags:

python

cython

I'm trying to run Hadoopy, which has a file _main.pyx, and import _main is failing with module not found in __init__.py.

I'm trying to run this on OS X w/ standard python 2.7.

like image 422
Dolan Antenucci Avatar asked Sep 22 '11 02:09

Dolan Antenucci


People also ask

How to import a function from a file in Python?

The process of importing a function from a file in Python is similar to importing modules. You have to create two files. Next, insert the definition of the function in one file and call the function from another. Name the new file myfile.py and insert a function.

How do I import a Python module in a different directory?

Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory. If you're using a Python version lower than 3.3, you can follow the steps in Import a File in a Different Directory instead. Let's say we move mymodule.py to a subdirectory called subdir:

How do you import a class in Python?

The import statement consists of the import keyword alongside the name of the module. Here we have created a class named GFG which has two methods: add () and sub (). Apart from that an explicit function is created named method () in the same python file.

How to call a function from another file in Python?

Name the new file myfile.py and insert a function. Create the second file, in the same directory, let’s call it main.py, and import the file and make a function call. import myfile myfile.myfunction ('Hello World!')


2 Answers

Add this code before you try to import _main:

import pyximport
pyximport.install()

Note that pyximport is part of Cython, so you'll have to install that if it isn't already.

like image 196
icktoofay Avatar answered Sep 19 '22 18:09

icktoofay


You need to make sure you have followed all steps:

  1. Install the Cython package using pip

    pip install Cython
    
  2. Create a Cython file bbox.pyx

    cimport cython
    import numpy as np
    cimport numpy as np
    
    DTYPE = np.float32
    ctypedef np.float32_t DTYPE_t
    
    @cython.boundscheck(False)
    def compare_bboxes(
           np.ndarray[DTYPE_t, ndim=2] boxes1,
           np.ndarray[DTYPE_t, ndim=2] boxes2):
     ...
    
  3. Create setup.py in the same directory

    from distutils.core import setup, Extension
    from Cython.Build import cythonize
    import numpy
    
    package = Extension('bbox', ['bbox.pyx'], include_dirs=[numpy.get_include()])
    setup(ext_modules=cythonize([package]))
    
  4. Build the Cython

    python3 setup.py build_ext --inplace
    
  5. Create your main python script run.py in the same directory

    import pyximport
    pyximport.install(setup_args={"script_args" : ["--verbose"]})
    from bbox import compare_bboxes
    
    def main(args):
       boxes1 = args.boxes1
       boxes2 = args.boxes2
       result = compare_bboxes(boxes1, boxes2)
    
  6. Run your main script in the same directory

    python run.py
    
like image 35
tsveti_iko Avatar answered Sep 20 '22 18:09

tsveti_iko