简体   繁体   English

PyCharm无法识别.pyx Cython文件

[英]PyCharm does not recognize a .pyx Cython file

I am trying to invoke a Cython file in a Python script. 我正在尝试在Python脚本中调用Cython文件。 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. 但是,即使我已经成功编译了C代码,PyCharm在执行导入命令时也无法识别Cython文件“ hello.pyx”,如下面的屏幕快照所示。 What is the remedy? 有什么补救办法?

在此处输入图片说明

The Cython file hello.c is generated by setup.py the content of which is shown below. Cython文件hello.csetup.py生成,其内容如下所示。

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

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

When you compile your module with cythonize, the resulting module will be put into a subfolder. 当您使用cythonize编译模块时,生成的模块将放入子文件夹中。 By default, this subfolder is not in the PATHS list. 默认情况下,此子文件夹不在“路径”列表中。 To fix this problem you can just move the resulting file .dll file into the folder where your hello.py and hello.pyx are located. 要解决此问题,您只需将生成的.dll文件移动到hello.py和hello.pyx所在的文件夹中。

An alternative approach is to add Extension like so: 另一种方法是添加扩展,如下所示:

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)
      )

And then compile with: 然后编译:

python setup.py build_ext --inplace

This code will put resulting .dll into the same folder as your source files. 此代码会将生成的.dll与源文件放入同一文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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