在C中嵌入python,未定义符号:PyExc_ImportError
我正在尝试为 Audacious Media Player 编写一个加载 python 模块的插件。 python 嵌入代码来自 python-2.6
源代码(embed/Demo)。这使用命令行进行编译,
gcc -o demo demo.c -lpython2.6 -lm -L/usr/lib/python2.6/config
我添加了 -lpython2.6 -lm -L/usr/lib/python2.6/config
到 CC 参数。
它加载一个导入 pygtk 和 gtk 模块的 Python 脚本,效果很好。
但是在我编译插件(共享库)后,出现以下错误(这不是特定于 gtk
我发现的,对于任何使用本机库的 python 模块都是一样的
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "./xyz.py", line 7, in <module>
import gtk
File "/usr/lib/pymodules/python2.6/gtk-2.0/gtk/__init__.py", line 30, in <module>
import gobject as _gobject
File "/usr/lib/pymodules/python2.6/gtk-2.0/gobject/__init__.py", line 26, in <module>
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
File "/usr/lib/pymodules/python2.6/gtk-2.0/glib/__init__.py", line 22, in <module>
from glib._glib import *
ImportError: /usr/lib/libpyglib-2.0-python2.6.so.0: undefined symbol: PyExc_ImportError
)设置 python 解释器的代码仅缺少 PySys_SetArgv
调用。我试图伪造它,但它导致了同样的错误!
I am trying to write a plug-in for Audacious Media Player that loads a python module. The python embedding code is from the python-2.6
source(embed/Demo). This compiles with the command line,
gcc -o demo demo.c -lpython2.6 -lm -L/usr/lib/python2.6/config
I added -lpython2.6 -lm -L/usr/lib/python2.6/config
to the CC args.
And it loads a Python script which imports pygtk
and gtk
modules, this works fine.
But after I compile the plug-in(a shared library) the following error occurs(this is not specific for gtk
as I found out, it's the same for any python module that uses native libraries)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "./xyz.py", line 7, in <module>
import gtk
File "/usr/lib/pymodules/python2.6/gtk-2.0/gtk/__init__.py", line 30, in <module>
import gobject as _gobject
File "/usr/lib/pymodules/python2.6/gtk-2.0/gobject/__init__.py", line 26, in <module>
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
File "/usr/lib/pymodules/python2.6/gtk-2.0/glib/__init__.py", line 22, in <module>
from glib._glib import *
ImportError: /usr/lib/libpyglib-2.0-python2.6.so.0: undefined symbol: PyExc_ImportError
The C code for setting up python interpreter only lacks the PySys_SetArgv
call. I tried to fake it but it resulted in the same error!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是 Linux,则需要将
-Xlinker -export-dynamic
添加到编译器行。这将使可执行文件中定义的符号可供扩展模块使用。在其他平台上,查看 Python makefile 中是否设置了
LINKFORSHARED
,然后使用相同的标志。Assuming you are on Linux, you need to add
-Xlinker -export-dynamic
to the compiler line. This will cause symbols defined in the executable to be available to extension modules.On other platforms, see whether
LINKFORSHARED
is set in the Python makefile, and then use the same flags.你在 Windows 上使用 64 位 python 吗? 64 位 python26.Lib 缺少一些在 32 位 python 中可用的符号。尝试 32 位 python,您的问题应该得到解决。
Are you using 64 bit python on Windows? 64 bit python26.Lib is missing some symbols which are available in 32 bit python. Try 32 bit python and your problem should be resolved.