简体   繁体   English

Linux上的MATLAB MEX文件无法在libpython.2.7.so中找到符号

[英]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:

Shell 贝壳

$ LD_DEBUG_OUTPUT=./ld_debug_out LD_DEBUG=all python -c 'import numpy'

MATLAB MATLAB

>> !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.

相关问题 在制作二进制文件时,PyInstaller找不到libpython2.7.so? - PyInstaller cannot find libpython2.7.so when making binary? 如何安装libpython2.7.so - How to install libpython2.7.so Docker:libpython2.7.so.1.0:无法打开共享对象文件:没有这样的文件或目录 - Docker: libpython2.7.so.1.0: cannot open shared object file: No such file or directory libpython2.7.so.1.0:无法打开共享对象文件:没有这样的文件或目录 - libpython2.7.so.1.0: cannot open shared object file: No such file or directory Python3.3在Linux(pip-3.3)中找不到libpython3.3m.so - Python3.3 can't find libpython3.3m.so in linux (pip-3.3) 在Python 2.7.13 Heroku dynos上缺少libpython2.7.so.1.0 - Missing libpython2.7.so.1.0 on Python 2.7.13 Heroku dynos yum未找到libpython2.7.so.1.0()(64bit) - libpython2.7.so.1.0()(64bit) not found by yum 如何链接到libpython.a,以便运行时链接程序可以在libpython.a中找到所有符号? - How can I link against libpython.a such that the runtime linker can find all the symbols in libpython.a? Anaconda Python virtualdev在Windows的Linux子系统上找不到libpython3.5m.so.1.0(Ubuntu 14.04) - Anaconda Python virtualdev can't find libpython3.5m.so.1.0 on Windows Subsystem for Linux (Ubuntu 14.04) 从AIX5.3上的Source构建的Python 2.7无法为libpython2.7.so执行 - Python 2.7 built from Source on AIX5.3 does not execute for libpython2.7.so
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM