PyCharm does not recognize a .pyx Cython file

时间:2019-01-09 22:01:46

标签: python cython

I am trying to invoke a Cython file in a Python script. I have read this answer and followed the instruction. However, even though I have successfully compiled the C code, PyCharm does not recognize the Cython file 'hello.pyx' while executing the import command, as shown in the screenshot below. What is the remedy?

enter image description here

The Cython file hello.c is generated by setup.py the content of which is shown below.

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

setup(name='Hello world app',
      ext_modules=cythonize("hello.pyx")
      )

1 个答案:

答案 0 :(得分:0)

使用cythonize编译模块时,结果模块将放入子文件夹中。默认情况下,此子文件夹不在“路径”列表中。要解决此问题,您只需将生成的.dll文件移动到hello.py和hello.pyx所在的文件夹中即可。

另一种方法是像这样添加扩展名:

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

from distutils.extension import Extension
exts = [Extension(name='hello',sources=['hello.pyx'])]

setup(name='Hello world app',
      ext_modules=cythonize(exts)
      )

然后使用:

python setup.py build_ext --inplace

此代码会将生成的.dll与源文件放在同一文件夹中。