我有一个conda环境,使用Cython时会出现错误。我无法在不使错误消失的情况下减少依赖关系列表,所以这里是完整的环境:
# 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在这种情况下看不到它。
答案 0 :(得分:0)
这实际上是由conda-forge中Python 3.7.6的不完整版本引起的。已修复。