简体   繁体   中英

Import Error while importing a Cython .pyx file

I have a python file which imports multiple packages using the following code:

from pyzoltan.core.carray import UIntArray, DoubleArray
from pyzoltan.core import zoltan

The first two imports, ie UIntArray and DoubleArray are successfully imported but the third import ie zoltan gives ImportError .

The pyzoltan/core directory contains the following files:

|-- __init__.py
|-- __init__.pyc
|-- carray.pxd
|-- carray.pxd.mako
|-- carray.pyx
|-- carray.pyx.mako
|-- carray.so
|-- generator.py
|-- generator.pyc
|-- msstdint.h
|-- tests
|   |-- 3d_partition.py
|   |-- 3d_partition.pyc
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- __pycache__
|   |   |-- test_zoltan.cpython-27-PYTEST.pyc
|   |-- geometric_partitioner.py
|   |-- geometric_partitioner.pyc
|   |-- mesh.txt
|   |-- test_zoltan.py
|   |-- test_zoltan.pyc
|   |-- zcomm.py
|   |-- zcomm.pyc
|-- zoltan.pxd
|-- zoltan.pyx
|-- zoltan_comm.pxd
|-- zoltan_comm.pyx
|-- zoltan_dd.pxd
|-- zoltan_dd.pyx
|-- zoltan_utils.py
|-- zoltan_utils.pyc

Is the problem due to the absence of zoltan.mako and zoltan.so or something else?

You have carray.so but not zoltan.so . Whatever build process produced the former should be able to produce the latter as well (ie by running cython on zoltan.pyx and zoltan.pxd ).

You are missing the pyzoltan/core/zoltan.so file.

The .pyx file can be compiled by Cython to a .c file, which can be compiled by a C compiler to .so file (or .pyd file on Windows).

There are a number of ways to build Cython code. The normal/recommended way is to write a distutils/setuptools setup.py file. The contents could look something like this:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("zoltan.pyx")
)    

and then you could build the .so file by running

python setup.py build_ext --inplace

But really you shouldn't need to write your own setup.py. If you install the core dependencies (and making sure they satisfy the version requirements) then follow the installation instructions then you should have zoltan ready to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM