我一直在做一个项目,使用 libpython2.7.so 将 Python 解释器作为 MEX 文件嵌入到 MATLAB 中,但是每当我尝试导入作为扩展模块实现的 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
set 运行 MATLAB 的结果,首先使用LD_LIBRARY_PATH
MATLAB 的启动设置,然后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)