Reputation: 83207
test1.py contains:
import sys
print 'ok'
test2.py contains:
import sys
import numpy as np
print 'ok'
Both python test1.py
and python test2.py
work fine.
When I'm in MATLAB, system('python test1.py')
works while system('python test2.py')
doesn't work and throw the following error message:
>> system('python test2.py')
Traceback (most recent call last):
File "test.py", line 3, in <module>
import numpy as np
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 13, in <module>
from polynomial import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/polynomial.py", line 17, in <module>
from numpy.linalg import eigvals, lstsq
File "/usr/lib/python2.7/dist-packages/numpy/linalg/__init__.py", line 48, in <module>
from linalg import *
File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 23, in <module>
from numpy.linalg import lapack_lite
ImportError: /afs/csail.mit.edu/system/common/matlab/2012a/sys/os/glnxa64/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by /usr/lib/liblapack.so.3gf)
Full stack:
I use Matlab 2012a x64 on Ubuntu 12.04 with Python 2.7. Any idea why Python called from MATLAB fails to import non-standard Python libraries (here Numpy, but I had the same issue with Scipy)? Note that everything works fine on my Windows computer.
Upvotes: 3
Views: 1745
Reputation: 83207
In case you don't have permission to edit your MATLAB directory such as in my case where MATLAB is located on a shared drive, or you just want to temporarily change which libraries MATLAB should call, you can use the LD_PRELOAD trick, which allows you to have a file (typically a library) loaded before any other library.
For this question, the issue was that as Marcin pointed out NumPy requires a newer version of libgfortran.so.3
that the one provided by MATLAB, so we need to pre-load the system version of libgfortran.so.3
(which should be up-to-date and subsequently working with Numpy) with the following command in the shell:
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libgfortran.so.3 matlab
If you want to make sure that all the features of NumPy and SciPy are working with in MATLAB, you can create the file good_luck.py
containing:
import numpy
numpy.test('full')
import scipy
scipy.test('full')
Then in MATLAB call it with system: system('python good_luck.py')
.
If you need to preload more than one files, which is likely to happen, as usual use :
to concatenate them, e.g.:
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libgfortran.so.3:/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16: matlab
I would still prefer to have a solution that would force MATLAB to call Python and give it the same environment as if Python was called from shell. It's quite a pain to have to manually preload the libraries that the Python code needs, and I still don't understand why MATLAB by default forces Python to use its (outdated) libraries.
Upvotes: 1
Reputation: 238329
The same problem is on Ubuntu 14.04 x64 with Matlab R2013a.
The problem is that matlab comes with some of its own libraries, in this case libgfortran.so.3
instead of using the system ones. From what I googled they are usually outdated, and numpy requires newer versions. Thus there is a problem.
In Ubuntu, it was enough to change the libraries. Don't know how to do it on windows ( I guess it would be somehow similar), but in Ubuntu it is as follows:
First: go to:
`cd /usr/local/MATLAB/R2013a/sys/os/glnxa64`
Second: make backup of matlab's libgfortran.so.3
:
`sudo mv libgfortran.so.3 libgfortran.so.3.bck`
Third: sym link system library:
`sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3`
Upvotes: 2