Cython: 找不到符号 _PyCFunction_Check

1 投票
1 回答
1718 浏览
提问于 2025-04-17 13:42

我正在尝试让这个基础的 Cython 教程正常工作。所以我有了

hello.pyx

def say_hello_to(name):
    print("Hello %s!" % name)

还有 setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]

setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

但是,当我尝试编译时,出现了这个错误:

$ python setup.py build_ext --inplace
running build_ext
failed to import Cython: dlopen(/usr/local/lib/python2.7/site-packages/Cython/Compiler/Scanning.so, 2): Symbol not found: _PyCFunction_Check
  Referenced from: /usr/local/lib/python2.7/site-packages/Cython/Compiler/Scanning.so
  Expected in: flat namespace
 in /usr/local/lib/python2.7/site-packages/Cython/Compiler/Scanning.so
error: Cython does not appear to be installed

Cython 已经安装,并且 scanning.so 似乎确实包含了相关的符号:

$ nm -gl /usr/local/lib/python2.7/site-packages/Cython/Compiler/Scanning.so | grep _PyCFunction_Check
             U _PyCFunction_Check

有什么想法吗?我在使用 OS X 10.7.5 和 homebrew 的 python 2.7.3。

编辑:正如 @bdash 在下面评论所指出的,U _PyCFunction_Check 实际上意味着 _PyCFunction_Check 是未定义的。

1 个回答

3

解决办法其实很简单:我先把用brew安装的Python和用pip安装的Cython都卸载了,然后再重新安装了一遍,这样一切就正常了。

我觉得问题出在我当时只安装了Xcode,就用brew装了Python。在我安装Cython之前,我又安装了苹果的命令行工具。所以Python和Cython是在不同的环境下安装的,这可能导致了错误。

撰写回答