为什么MATLAB不能导入这个python库?

时间:2015-06-02 19:42:23

标签: python matlab linker shared-libraries dyld

我想在MATLAB中使用http://www.losonczylab.org/sima/1.2/

我可以从python中运行得很好。

我可以在MATLAB中导入示例依赖项。

$ /opt/local/bin/python 
Python 2.7.9 (default, Dec 13 2014, 15:13:49) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from sima import sequence 
>>> sequence 
<module 'sima.sequence' from '/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/sequence.pyc'> 
>>> from sima import ImagingDataset 
>>> ImagingDataset 
<class 'sima.imaging.ImagingDataset'> 
在MATLAB中

>> pyversion /opt/local/bin/python 
>> pyversion 

version: '2.7' 
executable: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Cont...' 
library: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib' 
home: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7' 
isloaded: 0 

>> import py.picos.tools.available_solvers 
>> available_solvers 

ans = 

Python list with no properties. 

['cvxopt', 'mosek7'] 

>> import py.numpy.empty 
>> empty(0) 

ans = 

Python ndarray with properties: 

T: [1x1 py.numpy.ndarray] 
base: [1x1 py.NoneType] 
ctypes: [1x1 py.numpy.core._internal._ctypes] 
data: [1x0 py.buffer] 
dtype: [1x1 py.numpy.dtype] 
flags: [1x1 py.numpy.flagsobj] 
flat: [1x1 py.numpy.flatiter] 
imag: [1x1 py.numpy.ndarray] 
itemsize: 8 
nbytes: 0 
ndim: 1 
real: [1x1 py.numpy.ndarray] 
shape: [1x1 py.tuple] 
size: 0 
strides: [1x1 py.tuple] 

[] 

>> import py.sima.sequence 
Error using import 
Import argument 'py.sima.sequence' cannot be found or cannot be imported. 

>> import py.sima.Sequence 
Error using import 
Import argument 'py.sima.Sequence' cannot be found or cannot be imported. 

>> import py.sima.Sequence.create 
Error using import 
Import argument 'py.sima.Sequence.create' cannot be found or cannot be imported. 

>> import py.sima.sequence.create 
Error using import 
Import argument 'py.sima.sequence.create' cannot be found or cannot be imported. 

>> import py.sima.sequence 
Error using import 
Import argument 'py.sima.sequence' cannot be found or cannot be imported. 

>> import py.sima.Sequence 
Error using import 
Import argument 'py.sima.Sequence' cannot be found or cannot be imported. 

>> import py.sima.ImagingDataset 
Error using import 
Import argument 'py.sima.ImagingDataset' cannot be found or cannot be imported. 

>> import py.sima.imagingdataset 
Error using import 
Import argument 'py.sima.imagingdataset' cannot be found or cannot be imported. 

>> import py.sima 
Error using import 
Import argument 'py.sima' cannot be found or cannot be imported. 

如何确定无法导入 sima 的原因?

sima 位于路径上:

>> pp = py.pprint.PrettyPrinter

pp = 

  Python PrettyPrinter with no properties.

    <pprint.PrettyPrinter instance at 0x129425bd8>

>> pp.pprint(py.sys.path)
['',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/readline',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Users/eflister/Library/Python/2.7/lib/python/site-packages',
 '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']

我想出了以下繁琐的方法来找到问题导入 - 有更好的方法吗?

  1. 定义模块,如http://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html
  2. 复制在从MATLAB无法加载的库中的源文件导入
  3. 注释可疑导入,直到MATLAB调用模块成功
  4. 重命名模块并重复(一旦成功加载,MATLAB必须重新启动才能看到新的更改)
  5. 我只需要这样做29次,以隔离sima中的以下有问题的导入。 :)

    from PIL import Image
    
    from skimage.measure import approximate_polygon
    from skimage.measure import find_contours
    from skimage.filter import threshold_otsu
    from skimage import transform as tf
    
    from sklearn.decomposition import FastICA
    
    import scipy.io
    from scipy.stats import uniform
    from scipy.linalg import inv
    from scipy.sparse.linalg import eigsh, eigs
    from scipy.sparse.linalg import inv
    from scipy import ndimage
    from scipy.ndimage import measurements
    
    import cv2
    
    # note the following are OK:
    from scipy.linalg import eig, eigh, norm
    from scipy import sparse
    from multiprocessing import Pool
    import multiprocessing.util as util
    

    在python中运行此代码显示没有导入异常:

    lines = """
    import h5py
    from PIL import Image
    
    from skimage.measure import approximate_polygon
    from skimage.measure import find_contours
    from skimage.filter import threshold_otsu
    from skimage import transform as tf
    
    from sklearn.decomposition import FastICA
    
    import scipy.io
    from scipy.stats import uniform
    from scipy.linalg import inv
    from scipy.sparse.linalg import eigsh, eigs
    from scipy.sparse.linalg import inv
    from scipy import ndimage
    from scipy.ndimage import measurements
    
    import cv2
    """
    
    def tryImport(i):
        try:
            exec(i)
        except ImportError as e:
            print i
            print e
            print
    
    map(tryImport, filter(lambda x: len(x) > 0, lines.splitlines()))
    

    但在MATLAB中,它给出了输出:

    import h5py
    cannot import name _errors
    
    from PIL import Image
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so, 2): Library not loaded: /opt/local/lib/libtiff.5.dylib
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/_imaging.so
      Reason: Incompatible library version: _imaging.so requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0
    
    from skimage.measure import approximate_polygon
    cannot import name _ni_support
    
    from skimage.measure import find_contours
    cannot import name _ni_support
    
    from skimage.filter import threshold_otsu
    cannot import name _ni_support
    
    from skimage import transform as tf
    cannot import name hough_ellipse
    
    from sklearn.decomposition import FastICA
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/_lbfgsb.so, 2): Symbol not found: __gfortran_transfer_character_write
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/_lbfgsb.so
      Expected in: /Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib
     in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/_lbfgsb.so
    
    import scipy.io
    cannot import name doccer
    
    from scipy.stats import uniform
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so, 2): Symbol not found: __gfortran_stop_numeric_f08
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so
      Expected in: /Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib
     in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so
    
    from scipy.sparse.linalg import eigsh, eigs
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so, 2): Symbol not found: __gfortran_transfer_array_write
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so
      Expected in: /Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib
     in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so
    
    from scipy.sparse.linalg import inv
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so, 2): Symbol not found: __gfortran_transfer_array_write
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so
      Expected in: /Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib
     in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/_arpack.so
    
    from scipy import ndimage
    cannot import name _ni_support
    
    from scipy.ndimage import measurements
    cannot import name _ni_support
    
    import cv2
    dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: /opt/local/lib/libtiff.5.dylib
      Referenced from: /opt/local/lib/libopencv_highgui.2.4.dylib
      Reason: Incompatible library version: libopencv_highgui.2.4.dylib requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0
    

    我发现这个方法显示了来自matlab的错误:

    >> py.importlib.import_module('sima')
    Python Error: cannot import name hough_ellipse
    

    那么从这里开始有什么希望吗?

    我真的不明白错误,我可以推测,其中一些是:

    matlab的libtiff.5.dylib提供版本6.0.0,但PIL和opencv需要&gt; = 8.0.0

    matlab的libgfortran.3.dylib不适用于scipy的_lbfgsb.so,_ufuncs.so,_arpack.so

    是否有某种方法可以针对MATLAB的库编译 scipy/PIL/opencv

    这会非常痛苦吗? :)

    我发现了以下第三方python-from-matlab桥梁:

    有其他人要考虑吗?对于这种情况最有希望的意见/经验?我在猜matpy ......

    pymex是no good

    matpy不好:

    >> py_shell
    py> import sima
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/__init__.py", line 6, in <module>
        from sima.imaging import ImagingDataset
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/imaging.py", line 26, in <module>
        import h5py
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/h5py/__init__.py", line 23, in <module>
        from . import _conv
      File "h5py/h5t.pxd", line 14, in init h5py._conv (/opt/local/var/macports/build/_opt_mports_dports_python_py-h5py/py27-h5py/work/h5py-2.5.0/h5py/_conv.c:6958)
      File "h5py/h5t.pyx", line 139, in init h5py.h5t (/opt/local/var/macports/build/_opt_mports_dports_python_py-h5py/py27-h5py/work/h5py-2.5.0/h5py/h5t.c:20282)
      File "h5py/h5t.pyx", line 73, in h5py.h5t.lockid (/opt/local/var/macports/build/_opt_mports_dports_python_py-h5py/py27-h5py/work/h5py-2.5.0/h5py/h5t.c:2562)
      File "h5py/h5t.pyx", line 42, in h5py.h5t.typewrap (/opt/local/var/macports/build/_opt_mports_dports_python_py-h5py/py27-h5py/work/h5py-2.5.0/h5py/h5t.c:2196)
    RuntimeError: Interface initialization failed (Not a datatype object)
    py> import sima
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/__init__.py", line 6, in <module>
        from sima.imaging import ImagingDataset
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/imaging.py", line 33, in <module>
        import sima.misc
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/misc/__init__.py", line 21, in <module>
        from skimage import transform as tf
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/transform/__init__.py", line 1, in <module>
        from ._hough_transform import (hough_ellipse, hough_line,
      File "skimage/transform/_hough_transform.pyx", line 13, in init skimage.transform._hough_transform (skimage/transform/_hough_transform.c:22321)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/draw/__init__.py", line 2, in <module>
        from .draw3d import ellipsoid, ellipsoid_stats
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/draw/draw3d.py", line 3, in <module>
        from scipy.special import (ellipkinc as ellip_F, ellipeinc as ellip_E)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/__init__.py", line 586, in <module>
        from ._ufuncs import *
    ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so, 2): Symbol not found: __gfortran_stop_numeric_f08
      Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so
      Expected in: /Applications/MATLAB_R2015a.app/sys/os/maci64/libgfortran.3.dylib
     in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/special/_ufuncs.so
    py> import sima
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/__init__.py", line 6, in <module>
        from sima.imaging import ImagingDataset
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/imaging.py", line 33, in <module>
        import sima.misc
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/misc/__init__.py", line 21, in <module>
        from skimage import transform as tf
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/transform/__init__.py", line 1, in <module>
        from ._hough_transform import (hough_ellipse, hough_line,
    ImportError: cannot import name hough_ellipse
    py> import sima
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/__init__.py", line 6, in <module>
        from sima.imaging import ImagingDataset
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/imaging.py", line 33, in <module>
        import sima.misc
      File "/Users/eflister/Library/Python/2.7/lib/python/site-packages/sima/misc/__init__.py", line 21, in <module>
        from skimage import transform as tf
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/skimage/transform/__init__.py", line 1, in <module>
        from ._hough_transform import (hough_ellipse, hough_line,
    ImportError: cannot import name hough_ellipse
    py> import picos.tools
    py> print picos.tools.available_solvers()
    ['cvxopt', 'mosek7']
    

    我开始认为蠕虫病毒在一个单独的过程中听起来不错......

    但是如何使用LD_PRELOAD启动更新的libgfortran和libtiff的matlab?

    我想要的版本位于:

    /opt/local/lib/libgcc/libgfortran.3.dylib 
    /opt/local/lib/libtiff.5.dylib
    

    在新的matlab会话中,以下内容没有帮助。

    >> setenv('DYLD_LIBRARY_PATH',['/opt/local/lib:' getenv('DYLD_LIBRARY_PATH')])
    >> getenv('DYLD_LIBRARY_PATH')
    
    ans =
    
    /opt/local/lib:/Applications/MATLAB_R2015a.app/sys/os/maci64:/Applications/MATLAB_R2015a.app/bin/maci64/../../Contents/MacOS:/Applications/MATLAB_R2015a.app/bin/maci64:/Applications/MATLAB_R2015a.app/extern/lib/maci64:/Applications/MATLAB_R2015a.app/runtime/maci64:/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./native_threads:/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./server:/Applications/MATLAB_R2015a.app/sys/java/jre/maci64/jre/lib/./lib/jli
    

    但!!!

    ~$ DYLD_INSERT_LIBRARIES=/opt/local/lib/libgcc/libgfortran.3.dylib:/opt/local/lib/libtiff.5.dylib /Applications/MATLAB_R2015a.app/bin/matlab
    

    解决所有导入问题!!!

    >> import py.sima
    Error using import
    Import argument 'py.sima' cannot be found or cannot be imported.
    
    >> import py.sima.Sequence % succeeds!
    >> import py.sima.ImagingDataset % succeeds!
    >> x = py.sima.ImagingDataset([1,2],'~')
    Python Error: ImagingDataset objects must be initialized with a list of sequences.
    
    >> x = py.numpy.random.random([2,3,4])
    
    x = 
    
      Python ndarray with properties:
    
               T: [1x1 py.numpy.ndarray]
            base: [1x1 py.NoneType]
          ctypes: [1x1 py.numpy.core._internal._ctypes]
            data: [1x192 py.buffer]
           dtype: [1x1 py.numpy.dtype]
           flags: [1x1 py.numpy.flagsobj]
            flat: [1x1 py.numpy.flatiter]
            imag: [1x1 py.numpy.ndarray]
        itemsize: 8
          nbytes: 192
            ndim: 3
            real: [1x1 py.numpy.ndarray]
           shape: [1x3 py.tuple]
            size: 24
         strides: [1x3 py.tuple]
    
        [[[ 0.4158531   0.18912596  0.20743597  0.92631168]
          [ 0.0371785   0.48093953  0.88221637  0.85687334]
          [ 0.2267605   0.79464779  0.97438662  0.47791076]]
    
         [[ 0.05602249  0.06596678  0.05691491  0.02706325]
          [ 0.28477145  0.74781928  0.56700236  0.14500885]
          [ 0.87999554  0.78932748  0.24473039  0.18383564]]]
    
    >> x = py.sima.Sequence.create('ndarray',x)
    The class "py.sima.Sequence" is undefined.
    Perhaps Java is not running.
    
    >> py.sima.segment.STICA()
    
    ans = 
    
      Python STICA with no properties.
    
        <sima.segment.stica.STICA object at 0x138e15890>
    
    >> py.sima.segment.PlaneCA1PC()
    
    ans = 
    
      Python PlaneCA1PC with no properties.
    
        <sima.segment.ca1pc.PlaneCA1PC object at 0x138e158d0>
    

    我们终于结束了。但为什么我不能让sima.Sequence.create()跑!??!

2 个答案:

答案 0 :(得分:3)

答:MATLAB在NOT代码和代码中支持/ python支持的一组显式限制。类

虽然你的愿望和将其识别为MATLAB不支持python无限制。

MATLAB已明确列出功能,但不支持。其中一个是对以下划线字符开头的对象名称的支持。

您发布的许多错误消息都证实了这一事实。

有关 UN-SUPPORTED 功能的更多信息,请参阅&gt;&gt;&gt;的 Limitations to Python Support

通过列表显示,如果您想重新设计/重新设计所有不符合这些UN-SUPPORTED功能的软件包,预算支付的巨额费用。

该怎么办?

如果你真的非常依赖这个双世界的MATLAB / python,你可能会对另一种如何让事情发挥作用的架构产生兴趣。

尝试通过 ZeroMQ 等集成流程到流程的消息传递,并开始使用分布式处理,每侧使用本机(工作)模块。

比导入的试验/错误快得多,永远不会飞。

答案 1 :(得分:0)

结合DYLD_INSERT_LIBRARIES,我们无法直接在MATLAB中触摸sima.Sequence,因为它具有带前导下划线的属性,如@ user3666197所述,MATLAB doesn't allow

但我们可以调用使用sima.Sequence的python函数没问题:

https://github.com/losonczylab/sima/issues/183#issuecomment-109097816

最后,甜蜜的成功!

<强>附录
在Windows上,问题不在libgfortran/libtiff中,而是在使用msvcr的情况下使用。

Windows python使用msvcr90.dll,但MATLAB使用msvcr100.dll

形状,替换
CDLL(find_library('c'))
CDLL('C:\\Program Files\\MATLAB\\R2015a\\bin\\win64\\msvcr100.dll')
使所有工作完成。
this answer似乎是一种自动确定路径的方法无论你从哪里调整。