开发者

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

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开发者_C百科 python 2.7.


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.


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
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜