我一直在使用project将Python解释器作为MEX文件嵌入到MATLAB中,使用libpython2.7.so,但每当我尝试导入作为扩展模块实现的Python模块时都会遇到问题。例如,尝试导入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);
无论是否在调用LD_LIBRARY_PATH
之前清除Py_Initialize()
,并且在MATLAB中调用ldd
(对于此示例中为itertools.so
),都会出现此问题不会产生任何(not found)
条消息。下面,我用LD_DEBUG=libs
集粘贴运行MATLAB的结果,首先用MATLAB的启动设置LD_LIBRARY_PATH
,然后在尝试导入之前运行setenv('LD_LIBRARY_PATH', '')
。
如何解决此问题,并允许动态加载扩展模块?
使用MATLAB-default 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);
清除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)
答案 0 :(得分:1)
尝试从MATLAB内部和系统shell上运行常规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'
并检查输出文件(可能all
有点太多信息!)。注意我导入了numpy
,因为itertools
在我的安装中没有相应的共享库..
一个想法是在调用Py_Initialize()
之前尝试手动加载MEX函数中的共享库:
dlopen('libpython2.7.so', RTLD_GLOBAL|RTLD_LAZY);
我在Windows机器上,但上次我玩这个时我也遇到了一些问题。我记得一旦导入,一些C扩展模块无法在同一个会话中卸载和重新加载(在嵌入Python的托管过程的生命周期中)。请参阅这些remarks和相关的PEP。