MATLAB Answers

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

11 views (last 30 days)
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()
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 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.
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 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 throws the following error:
>> import
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/", line 97, in <module>
from .matlab import loadmat, savemat, whosmat, byteordercodes
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/", line 13, in <module>
from .mio import loadmat, savemat, whosmat
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/", line 12, in <module>
from .miobase import get_matfile_version, docfiller
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/io/matlab/", line 22, in <module>
from scipy.misc import doccer
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/misc/", line 68, in <module>
from scipy.interpolate._pade import pade as _pade
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/interpolate/", line 175, in <module>
from .interpolate import *
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/interpolate/", line 21, in <module>
import scipy.special as spec
File "/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/", line 640, in <module>
from ._ufuncs import *
ImportError: dlopen(/Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/, 2): Symbol not found: __gfortran_stop_numeric_f08
Referenced from: /Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/
Expected in: /Applications/
in /Users/mws22/anaconda2/lib/python2.7/site-packages/scipy/special/
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!