为什么 MATLAB 不能导入这个 python 库?
why can't MATLAB import this python library?
我想在 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']
我想出了以下繁琐的方法来查找导入问题 -- 有更好的方法吗?
- 定义一个模块,如http://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html
- 复制 从 MATLAB 不会加载的库中的源文件导入
- 注释掉 怀疑导入,直到 MATLAB 调用模块成功
- 重命名 模块并重复(一旦成功加载,MATLAB 必须重新启动才能看到新的变化)
我只需要这样做 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需要>=8.0.0
matlab 的 libgfortran.3.dylib 不适用于 scipy 的 _lbfgsb.so、_ufuncs.so、_arpack.so
有什么方法可以针对 MATLAB 的库编译 scipy/PIL/opencv
吗?
会不会特别疼? :)
我找到了以下第 3 方 python-from-matlab 桥:
- http://algoholic.eu/matpy/ -- numpy.ndarray 整合
- https://github.com/kw/pymex -- numpy.ndarray集成,近期开发少,但也好看
- https://github.com/pv/pythoncall -- 更老,看起来更不精致
- https://code.google.com/p/python-matlab-wormholes/ -- 也是旧的,只有 ndarray,通过 TCP 双向
还有其他需要考虑的吗? opinions/experience 哪个最适合这种情况?我猜是 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()
到 运行!??!
答:MATLAB 有一组它确实支持的明确限制/NOT
在 python
代码和 类
中支持
虽然你们都希望并愿意让它一起工作,但得到认可,MATLAB
不支持 python
无限制。
MATLAB 已明确列出 功能,它不支持 。其中之一是支持以下划线字符开头的对象名称。
很多这样的错误信息,你已经发布了,确认这个事实。
更多关于 UN-SUPPORTED 功能的信息可以在 >>> Limitations to Python Support
中查看
通过列表显示,如果您想重新设计/重新设计所有不符合这些 UN-SUPPORTED 功能的软件包,需要支付的预算有多大。
怎么办?
如果您确实严重依赖这个双重世界 MATLAB/python,您可能会对另一种架构感兴趣,了解如何让事情正常进行。
尝试通过 ZeroMQ
等集成进程到进程的消息传递,并开始使用分布式处理,在每一侧使用本机(工作)模块.
在进口方面会比 trial/error 快得多,那永远不会飞。
结合 DYLD_INSERT_LIBRARIES
,我们不能直接在 MATLAB 中触及 sima.Sequence
,因为它具有带前导下划线的属性,如 @user3666197 所述,MATLAB doesn't allow。
但是我们可以调用 python 使用 sima.Sequence
的函数没问题:
https://github.com/losonczylab/sima/issues/183#issuecomment-109097816
终于,甜蜜的成功!
附录:
在 Windows 上,问题不在 libgfortran/libtiff
上,而是在 shapely 使用 msvcr
.
上
Windows python 使用 msvcr90.dll
,但 MATLAB 使用 msvcr100.dll
.
身材匀称,
正在更换
CDLL(find_library('c'))
with
CDLL('C:\Program Files\MATLAB\R2015a\bin\win64\msvcr100.dll')
使一切正常。
this answer 似乎是一种可以确定的方法自动路径,适用于您从哪里调用 shapely。
我想在 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']
我想出了以下繁琐的方法来查找导入问题 -- 有更好的方法吗?
- 定义一个模块,如http://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html
- 复制 从 MATLAB 不会加载的库中的源文件导入
- 注释掉 怀疑导入,直到 MATLAB 调用模块成功
- 重命名 模块并重复(一旦成功加载,MATLAB 必须重新启动才能看到新的变化)
我只需要这样做 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需要>=8.0.0
matlab 的 libgfortran.3.dylib 不适用于 scipy 的 _lbfgsb.so、_ufuncs.so、_arpack.so
有什么方法可以针对 MATLAB 的库编译 scipy/PIL/opencv
吗?
会不会特别疼? :)
我找到了以下第 3 方 python-from-matlab 桥:
- http://algoholic.eu/matpy/ -- numpy.ndarray 整合
- https://github.com/kw/pymex -- numpy.ndarray集成,近期开发少,但也好看
- https://github.com/pv/pythoncall -- 更老,看起来更不精致
- https://code.google.com/p/python-matlab-wormholes/ -- 也是旧的,只有 ndarray,通过 TCP 双向
还有其他需要考虑的吗? opinions/experience 哪个最适合这种情况?我猜是 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()
到 运行!??!
答:MATLAB 有一组它确实支持的明确限制/NOT
在 python
代码和 类
中支持
虽然你们都希望并愿意让它一起工作,但得到认可,MATLAB
不支持 python
无限制。
MATLAB 已明确列出 功能,它不支持 。其中之一是支持以下划线字符开头的对象名称。
很多这样的错误信息,你已经发布了,确认这个事实。
更多关于 UN-SUPPORTED 功能的信息可以在 >>> Limitations to Python Support
中查看通过列表显示,如果您想重新设计/重新设计所有不符合这些 UN-SUPPORTED 功能的软件包,需要支付的预算有多大。
怎么办?
如果您确实严重依赖这个双重世界 MATLAB/python,您可能会对另一种架构感兴趣,了解如何让事情正常进行。
尝试通过 ZeroMQ
等集成进程到进程的消息传递,并开始使用分布式处理,在每一侧使用本机(工作)模块.
在进口方面会比 trial/error 快得多,那永远不会飞。
结合 DYLD_INSERT_LIBRARIES
,我们不能直接在 MATLAB 中触及 sima.Sequence
,因为它具有带前导下划线的属性,如 @user3666197 所述,MATLAB doesn't allow。
但是我们可以调用 python 使用 sima.Sequence
的函数没问题:
https://github.com/losonczylab/sima/issues/183#issuecomment-109097816
终于,甜蜜的成功!
附录:
在 Windows 上,问题不在 libgfortran/libtiff
上,而是在 shapely 使用 msvcr
.
Windows python 使用 msvcr90.dll
,但 MATLAB 使用 msvcr100.dll
.
身材匀称,
正在更换
CDLL(find_library('c'))
with
CDLL('C:\Program Files\MATLAB\R2015a\bin\win64\msvcr100.dll')
使一切正常。
this answer 似乎是一种可以确定的方法自动路径,适用于您从哪里调用 shapely。