导入错误:.so:未定义符号:Py_InitModule4
我正在尝试为Python写一个C扩展模块,但遇到了标题中提到的错误。这个模块在Python 2.7.6下运行正常,但在Python 3.4.0下运行时就出错了。
我运行了 python setup.py build_ext --inplace
来编译代码。我的 setup.py
文件长这样:
from distutils.core import setup, Extension
setup(
ext_modules=[Extension("_file", ["_file.c", "file.c"])],
)
然后我启动Python3的解释器,输入 import _file
,结果是这样的:
>>>import _file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/name/python/file/_file.so: undefined symbol: Py_InitModule4
正如我之前提到的,在Python 2.7.6中做同样的事情是完全没问题的。我到底哪里搞错了呢?
1 个回答
0
Python 3.x的库和Python 2.x的库里并不是完全一样的,里面的符号(也就是一些功能和命令)可能会有差别。在使用之前,确保你把你的模块(就是你写的代码)重新编译一下,适配3.x版本。