我有一个 conda 环境,它在使用 Cython 时出现错误。我无法在不让 bug 消失的情况下减少依赖列表,所以这里是完整的环境:
# environment.yml
name: test
channels:
- conda-forge
- defaults
dependencies:
- pip
- compilers
- make
- setuptools
- cython
- daetk
- hdf5 =*=*mpich*
- h5py =*=*mpich*
- metis
- mpich
- numpy
- openblas
- parmetis
- petsc4py
- petsc
- python=3
- scorec
- superlu
- superlu_dist
- triangle
- pychrono
- mpi4py
- gmsh
- matplotlib
- mpi4py
- nose
- pytables
- pytest
- pytest-cov
- pytest-xdist
- scipy
- tetgen
- ncurses
- pychrono
- python=3
- future
- ipyparallel
- pillow
- recordtype
创建环境:conda env create -f environment.yml
激活它:conda activate test
创建一个文件helloworld.pyx
:
print("Hello World")
创建一个setup.py
:
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
)
Cythonize它:python setup.py build_ext --inplace
使用 导入时python -c "import helloworld"
,会出现以下错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'helloworld'
它创建了一个文件helloworld.cpython-37-x86_64-linux-gnu.so
,它应该在的位置helloworld.cpython-37m-x86_64-linux-gnu.so
。事实上,如果你重命名它,你可以验证它是否正常工作:
mv helloworld.cpython-37-x86_64-linux-gnu.so helloworld.cpython-37m-x86_64-linux-gnu.so
在 conda 环境中m
存在后缀:$CONDA_PREFIX/include/python3.7m
. 我知道这与 Python 是否用 pymalloc 编译有关,但我不明白为什么 Cython 在这种情况下看不到它。