gpt4 book ai didi

c - undefined symbol : PyExc_ImportError when embedding Python in C

转载 作者:太空狗 更新时间:2023-10-29 15:24:45 27 4
gpt4 key购买 nike

我正在开发一个调用 python 脚本的 C 共享库。当我运行该应用程序时,出现此错误:

Traceback (most recent call last):
File "/home/ubuntu/galaxy-es/lib/galaxy/earthsystem/gridftp_security/gridftp_acl_plugin.py", line 2, in <module>
import galaxy.eggs
File "/home/ubuntu/galaxy-es/lib/galaxy/eggs/__init__.py", line 5, in <module>
import os, sys, shutil, glob, urllib, urllib2, ConfigParser, HTMLParser, zipimport, zipfile
File "/usr/lib/python2.7/zipfile.py", line 6, in <module>
import io
File "/usr/lib/python2.7/io.py", line 60, in <module>
import _io
ImportError: /usr/lib/python2.7/lib-dynload/_io.so: undefined symbol: PyExc_ImportError

如果我尝试从控制台导入模块 io 则工作正常:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import galaxy.eggs
>>>

在库的编译过程中,我按照这里的建议使用了这个编译器选项: Embedding python in C, undefined symbol: PyExc_ImportError此外,我还添加了从python-config --includes|--libs|--cflags|--ldflags

这里可以找到库的makefile日志http://pastebin.com/348rhBjM

非常感谢,我们将不胜感激。

最佳答案

@user1515248 solution是一个仅链接的解决方案,它是 discouraged .我写这个答案是为了扩展他提供的链接并提供更充实的答案(这也支持他提供的链接)。

链接,https://mail.python.org/pipermail/new-bugs-announce/2008-November/003322.html , 说:

I have been given the following workaround: in mylib.c, before PyInitialize() I can call dlopen("libpython2.5.so", RTLD_LAZY | RTLD_GLOBAL);

This works, but I believe that lib-dynload/*.so should depend on libpython2.5.so.1 so this hack should not be necessary.

I am using Ubuntu 8.04 with Python version 2.5.2-2ubuntu4.1.

我所要做的就是添加一行代码:

// new line of code
void*const libpython_handle = dlopen("libpython2.6.so", RTLD_LAZY | RTLD_GLOBAL);

PyInitialize();

附注我在 CentOS-6 上。

p.p.s.我的 PyInitialize() 包装在一个类中,因此 dlopen()/PyInitialize() 在构造函数中完成,dlclose( )/PyFinalize() 在析构函数中完成。

关于c - undefined symbol : PyExc_ImportError when embedding Python in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11842920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com