python - Cython 构建 python 脚本

标签 python cython

尝试让 cython 运行,在遵循快速入门指南后我遇到了这个问题:

$ ~/midt $ ./setup.py build_ext --inplace
running build_ext
building 'proc' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c proc.c -o build/temp.linux-x86_64-2.7/proc.o
proc.c:2511:16: error: ‘initproc’ redeclared as different kind of symbol
 PyMODINIT_FUNC initproc(void); /*proto*/
                ^
In file included from /usr/include/python2.7/Python.h:80:0,
                 from proc.c:16:
/usr/include/python2.7/object.h:320:15: note: previous declaration of ‘initproc’ was here
 typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
               ^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

这是我的版本号和内容:

$ ~/midt $ python --version
Python 2.7.6
$ ~/midt $ cython --version
Cython version 0.23.5
$ ~/midt $ uname -a
Linux xxxxx 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

最佳答案

我认为问题是:

  1. Python 需要调用初始化 C 模块初始化的函数 init<module_name> (您替换模块名称的位置)。 (Python 3 使用略有不同的形式)。

  2. 您已将模块命名为 proc .

  3. Cython 因此创建了一个名为 initproc 的函数在导入模块时定义。

  4. Python 定义了一个名为 initproc 的 typedef在内部,这与 Cython 生成的 initproc 冲突用于初始化您的模块。

解决方案是将您的模块命名为 proc 以外的名称。这不是一个理想的解决方案,但没有太多其他选择。


Python 3 选择不同形式的原因之一 PyInit_<module_name>众所周知,Python 2 形式会导致一些冲突(请参阅 https://www.python.org/dev/peps/pep-3121/#entry-point-name-conflicts )。

关于python - Cython 构建 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36314266/

相关文章:

python - NaN 根据 Python 启动给出错误?

python - BK-Tree如何优化

python - 使用 Cython 从 header 导出常量

python - 非常慢的 Numpy 缓冲区指针访问

python - 如何在 tensorflow 中可视化估计器训练模型?

python - 在Python中计算字符串中的重复字符

python - R 脚本无法从 bash 启动直方图

python - 如何将给定字符串列表中的每个单词大写?

python - pandas 应用的可能加速

python - 在 cython 中指定大于 long double 的数字