MATLAB Answers

How does Python package generated in Matlab allow for "import matlab"?

11 views (last 30 days)
Michael
Michael on 7 Aug 2018
Commented: Michael on 10 Aug 2018
I am developing a Python package from my Matlab functions using Matlab Compiler SDK within R2018a. I used this tutorial. I got the basic functionality down and executed the functions in Python, but I am now working on input/outputs data types. Some of the help files reference the Matlab array classes via 'import matlab' in python. For example, a Python code might look like:
import PythonSimple
import matlab
a = matlab.double([[1,2,3], [4,5,6]])
blah = PythonSimple.initialize()
print(blah.python_trial(a))
blah.terminate()
where PythonSimple is Python package I compiled from Matlab code. My question, when is the python package 'matlab' installed for python. ie. Why is it that the 'import matlab' works on my machine even though I never had to do a 'pip install matlab' or equivalent? Do I need Matlab on my machine to call 'import matlab' or does the matlab package get installed for Python when I install my compiled packages Python simple (python PythonSimple.py install)? I need to know because the systems on which I eventually will deploy PythonSimple will not have Matlab installed, but it would be helpful if I could use the matlab package Python data types within my Python scripts to simplify the input/output data type conversions.
Thanks.
  2 Comments
Michael
Michael on 10 Aug 2018
I am using that method, although I didn't need to install MATLAB Runtime because I already had it on my machine. My understanding has evolve a bit in the last few days after realizing installing the MATLAB runtime was required for Python package installation on machines without MATLAB installed. I am using python setup.py install to install my packages, although I have to install them to a non-default directory.
I do need to know though, in the link you provided, step 6 says to "set the required environmental variables:"
On Linux®: setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:mcrroot/runtime/glnxa64: mcrroot/bin/glnxa64:mcrroot/sys/os/glnxa64: mcrroot/sys/opengl/lib/glnxa64
On OS X: setenv DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH}:mcrroot/runtime/maci64: mcrroot/sys/os/maci64:mcrroot/bin/maci64
Are those command entered in terminal, in Matlab command window, or somewhere else? I am continuing to have problems with getting other non-matlab generated packages to import in my python scripts when using mwpython (the import fine in python). For example, the python command import scip exectures just fine, but import scipy.io throws the following error:
>> import scipy.io
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/__init__.py", line 97, in <module>
from .matlab import loadmat, savemat, whosmat, byteordercodes
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/__init__.py", line 13, in <module>
from .mio import loadmat, savemat, whosmat
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/mio.py", line 12, in <module>
from .miobase import get_matfile_version, docfiller
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/miobase.py", line 22, in <module>
from scipy.misc import doccer
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/misc/__init__.py", line 68, in <module>
from scipy.interpolate._pade import pade as _pade
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/interpolate/__init__.py", line 175, in <module>
from .interpolate import *
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 21, in <module>
import scipy.special as spec
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/__init__.py", line 640, in <module>
from ._ufuncs import *
ImportError: dlopen(/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/_ufuncs.so, 2): Symbol not found: __gfortran_stop_numeric_f08
Referenced from: /Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/_ufuncs.so
Expected in: /Applications/MATLAB_R2018a.app/sys/os/maci64/libgfortran.3.dylib
in /Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/_ufuncs.so
It think this might be related to the DYLD_LIBRARY_PATH not being set, but I don't know and can't seem to find any resources online about this.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!