【发布时间】:2017-06-15 01:17:46
【问题描述】:
我正在尝试关注tutorial 使用 pyx、pxd 和 cimport 来创建和使用扩展类型。
在终端中编译 Cython 文件时,我收到一个我不知道如何纠正的错误:
pyx 文件中的cdef class CythonClass: 表示为错误行。
File "/Library/Python/2.7/site-packages/Cython/Build/Dependencies.py", line 1056, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: CythonClass.pyx
我在 MacOS Sierra 上使用 Cython 版本 .25(并且也尝试过其他版本,每个版本都安装有 pip install cython)。 Python 版本是 2.7.10。
按照建议,我安装了 gcc(Xcode 8.2 的命令行工具),但仍然收到错误消息。
文件内容:
像素:
cdef class CythonClass:
cdef:
list list1
dict d1, d2, d3, d4, d5, d6
pyx:
cdef class CythonClass:
def __init__(self):
self.list1 = []
self.d1 = {}
self.d2 = {}
self.d3 = {}
self.d4 = {}
self.d5 = {}
self.d6 = {}
setup.py(由终端中的python setup.py build_ext --inplace 调用):
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["CythonClass.pyx"]))
至少目前,我将尝试在不使用 pxd 文件的情况下进行编译,因为扩展类型的编译已经完成。但是,导入扩展类型的主函数没有编译(错误:extTypeName is not a type identifier)。
【问题讨论】:
-
嗨 - 我刚刚在我的机器上试过这个,但实际上我在编译时没有收到任何错误。我使用 Windows 10,Python 3.5 64 位,Cython 版本 0.24.1。我不确定您的问题的原因是什么。对不起!
-
你是如何安装 Cython 的?该日志使您看起来像是在使用系统 Python 并在系统范围内安装 Cython,但这并没有说明如何。 Cython 文档docs.cython.org/en/latest/src/quickstart/install.html 建议安装 Apple 的 XCode 以获得 gcc 版本。
-
您好,我也测试了,没有发现错误。问题可能在于烦人的细节,例如路径中的文件名或源代码中存在不可见字符。能否附上文件并提供完整的错误日志?
-
更多细节:Python 中不允许在模块名中使用连字符
-。 -
只有在要将 cython 模块导入另一个 cython 模块时才需要 pxd 文件。但是你不会在
CythonClass.pyx中导入CythonClass.pxd。
标签: python compiler-errors compilation cython