[英]MATLAB MEX file on Linux fails to find symbols in libpython.2.7.so
I've been working on a project to embed the Python interpreter into MATLAB as a MEX-file, using libpython2.7.so, but encounter a problem whenever I try to import Python modules implemented as extension modules. 我一直在一个项目中,使用libpython2.7.so将Python解释器作为MEX文件嵌入到MATLAB中,但是每当我尝试导入实现为扩展模块的Python模块时,都会遇到问题。 For example, attempting to import
itertools
results in the following error: 例如,尝试导入
itertools
导致以下错误:
>> py_import itertools
ImportError: /usr/lib64/python2.7/lib-dynload/itertoolsmodule.so: undefined symbol: PyTuple_Type
??? Error using ==> pymex_fns
Python exception inside py_import.
Error in ==> py_import at 24
py_obj = pymex_fns(py_function_t.IMPORT, name);
This behavior occurs whether or not I clear LD_LIBRARY_PATH
before the call to Py_Initialize()
, and a call to ldd
(for itertools.so
, in this example) from within MATLAB doesn't result in any (not found)
messages. 无论我是否在调用
Py_Initialize()
之前清除LD_LIBRARY_PATH
以及从MATLAB内部对ldd
的调用(对于本示例中为itertools.so
)都不会导致任何(not found)
消息,都会发生此行为。 Below, I've pasted the results of running MATLAB with LD_DEBUG=libs
set, first with LD_LIBRARY_PATH
set by MATLAB's startup, and then after running setenv('LD_LIBRARY_PATH', '')
before attempting to import. 下面,我粘贴了使用
LD_DEBUG=libs
设置运行MATLAB的结果,首先通过MATLAB的启动设置了LD_LIBRARY_PATH
,然后在尝试导入之前运行setenv('LD_LIBRARY_PATH', '')
之后粘贴了结果。
How can I fix this problem, and allow for the dynamic loading of extension modules? 如何解决此问题,并允许动态加载扩展模块?
With MATLAB-default LD_LIBRARY_PATH
: 使用MATLAB默认的
LD_LIBRARY_PATH
:
>> py_import itertools
3018: find library=libpython2.7.so.1.0 [0]; searching
3018: search path=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64/libpython2.7.so.1.0
3018: search path=/usr/local/MATLAB/R2011a/sys/os/glnxa64 (LD_LIBRARY_PATH)
3018: trying file=/usr/local/MATLAB/R2011a/sys/os/glnxa64/libpython2.7.so.1.0
3018: search path=/usr/local/MATLAB/R2011a/bin/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libpython2.7.so.1.0
3018: search path=/usr/local/MATLAB/R2011a/extern/lib/glnxa64:/usr/local/MATLAB/R2011a/runtime/glnxa64:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64 (LD_LIBRARY_PATH)
3018: trying file=/usr/local/MATLAB/R2011a/extern/lib/glnxa64/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/runtime/glnxa64/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server/libpython2.7.so.1.0
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/libpython2.7.so.1.0
3018: search cache=/etc/ld.so.cache
3018: trying file=/lib64/libpython2.7.so.1.0
3018:
3018: find library=libutil.so.1 [0]; searching
3018: search path=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64/libutil.so.1
3018: search path=/usr/local/MATLAB/R2011a/sys/os/glnxa64 (LD_LIBRARY_PATH)
3018: trying file=/usr/local/MATLAB/R2011a/sys/os/glnxa64/libutil.so.1
3018: search path=/usr/local/MATLAB/R2011a/bin/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3018: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libutil.so.1
3018: search path=/usr/local/MATLAB/R2011a/extern/lib/glnxa64:/usr/local/MATLAB/R2011a/runtime/glnxa64:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64 (LD_LIBRARY_PATH)
3018: trying file=/usr/local/MATLAB/R2011a/extern/lib/glnxa64/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/runtime/glnxa64/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server/libutil.so.1
3018: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/libutil.so.1
3018: search cache=/etc/ld.so.cache
3018: trying file=/lib64/libutil.so.1
3018:
3018:
3018: calling init: /lib64/libutil.so.1
3018:
3018:
3018: calling init: /lib64/libpython2.7.so.1.0
3018:
3018:
3018: calling init: /home/cgranade/academics/software-projects/pymex-embed/src/pymex_fns.mexa64
3018:
3018: /home/cgranade/academics/software-projects/pymex-embed/src/pymex_fns.mexa64: error: symbol lookup error: undefined symbol: mexLibrary (fatal)
3018: /usr/lib64/python2.7/lib-dynload/itertoolsmodule.so: error: symbol lookup error: undefined symbol: PyTuple_Type (fatal)
??? Error using ==> pymex_fns
Python exception inside py_import.
Error in ==> py_import at 24
py_obj = pymex_fns(py_function_t.IMPORT, name);
With LD_LIBRARY_PATH
cleared: 清除
LD_LIBRARY_PATH
:
>> py_import itertools
3125: find library=libpython2.7.so.1.0 [0]; searching
3125: search path=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64/libpython2.7.so.1.0
3125: search path=/usr/local/MATLAB/R2011a/sys/os/glnxa64 (LD_LIBRARY_PATH)
3125: trying file=/usr/local/MATLAB/R2011a/sys/os/glnxa64/libpython2.7.so.1.0
3125: search path=/usr/local/MATLAB/R2011a/bin/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libpython2.7.so.1.0
3125: search path=/usr/local/MATLAB/R2011a/extern/lib/glnxa64:/usr/local/MATLAB/R2011a/runtime/glnxa64:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64 (LD_LIBRARY_PATH)
3125: trying file=/usr/local/MATLAB/R2011a/extern/lib/glnxa64/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/runtime/glnxa64/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server/libpython2.7.so.1.0
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/libpython2.7.so.1.0
3125: search cache=/etc/ld.so.cache
3125: trying file=/lib64/libpython2.7.so.1.0
3125:
3125: find library=libutil.so.1 [0]; searching
3125: search path=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64:/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../bin/glnxa64/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64/libutil.so.1
3125: search path=/usr/local/MATLAB/R2011a/sys/os/glnxa64 (LD_LIBRARY_PATH)
3125: trying file=/usr/local/MATLAB/R2011a/sys/os/glnxa64/libutil.so.1
3125: search path=/usr/local/MATLAB/R2011a/bin/glnxa64 (RPATH from file /usr/local/MATLAB/R2011a/bin/glnxa64/MATLAB)
3125: trying file=/usr/local/MATLAB/R2011a/bin/glnxa64/libutil.so.1
3125: search path=/usr/local/MATLAB/R2011a/extern/lib/glnxa64:/usr/local/MATLAB/R2011a/runtime/glnxa64:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64 (LD_LIBRARY_PATH)
3125: trying file=/usr/local/MATLAB/R2011a/extern/lib/glnxa64/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/runtime/glnxa64/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server/libutil.so.1
3125: trying file=/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/libutil.so.1
3125: search cache=/etc/ld.so.cache
3125: trying file=/lib64/libutil.so.1
3125:
3125:
3125: calling init: /lib64/libutil.so.1
3125:
3125:
3125: calling init: /lib64/libpython2.7.so.1.0
3125:
3125:
3125: calling init: /home/cgranade/academics/software-projects/pymex-embed/src/pymex_fns.mexa64
3125:
3125: /home/cgranade/academics/software-projects/pymex-embed/src/pymex_fns.mexa64: error: symbol lookup error: undefined symbol: mexLibrary (fatal)
3125: /usr/lib64/python2.7/lib-dynload/itertoolsmodule.so: error: symbol lookup error: undefined symbol: PyTuple_Type (fatal)
Try running the regular python both from inside MATLAB and on the system shell: 尝试从MATLAB内部和系统外壳上运行常规python:
$ LD_DEBUG_OUTPUT=./ld_debug_out LD_DEBUG=all python -c 'import numpy'
>> !LD_DEBUG_OUTPUT=./ld_debug_out LD_DEBUG=all python -c 'import numpy'
and inspect the output files (maybe all
is a bit too much info!). 并检查输出文件(也许
all
信息all
太多了!)。 Note I imported numpy
instead since itertools
does not have a corresponding shared library on my installation.. 注意我改为导入
numpy
因为itertools
在安装中没有相应的共享库。
One idea is to try to manually load the shared libraries in your MEX function before calling Py_Initialize()
: 一种想法是尝试在调用
Py_Initialize()
之前在MEX函数中手动加载共享库:
dlopen('libpython2.7.so', RTLD_GLOBAL|RTLD_LAZY);
I am on a Windows machine, but last time I played with this I had some problems as well. 我在Windows计算机上,但是上一次我玩这个游戏时,我也遇到了一些问题。 I remember that once imported, some C extension modules could not be unloaded and reloaded in the same session (during the lifetime of the hosting process where Python is embedded).
我记得,一旦导入,某些C扩展模块就无法在同一会话中卸载和重新加载(在嵌入Python的托管过程的生存期内)。 See these remarks and related PEP .
请参阅这些说明和相关的PEP 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.