normally_deviated
normally_deviated

Reputation: 105

Calling a python script via a shell script via Matlab's system() causes bizarre error

So I have Matlab "calling out" to an external python library. The Matlab side writes out a matrix of doubles to a file using dlmwrite(), builds up a command string (it's just one line) by concatenation to the tune of:

#/bin/tcsh
python myscript.py -option1 long_file_path1 long_file_path2 long_file_path3<br>

This long horrible command is written that out to a shell script, lets name it caller.sh, and then Matlab successfully calls chmod 755 on caller.sh, using the system() command.

However, when I then try to actually run the system() command on the shell script (ie. system(['sh caller.sh']), it crashes with an almighty bang:

Traceback (most recent call last):
  File "/homes/sb1006/PhD_Year_1/code//my_libs/LSTM/python/monohiddenlayer_regression_LSTM.py", line 1, in <module>
    import pybrain
  File "/homes/sb1006/PhD_Year_1/code/my_libs/LSTM/python/pybrain/__init__.py", line 1, in <module>
    from structure.__init__ import *
  File "/homes/sb1006/PhD_Year_1/code/my_libs/LSTM/python/pybrain/structure/__init__.py", line 1, in <module>
    from connections.__init__ import *
  File "/homes/sb1006/PhD_Year_1/code/my_libs/LSTM/python/pybrain/structure/connections/__init__.py", line 1, in <module>
    from full import FullConnection
  File "/homes/sb1006/PhD_Year_1/code/my_libs/LSTM/python/pybrain/structure/connections/full.py", line 3, in <module>
    from scipy import reshape, dot, outer
  File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 78, in <module>
    from numpy import show_config as show_numpy_config
  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: /usr/lib/matlab/R2012a/sys/os/glnxa64/libgfortran.so.3: version 'GFORTRAN_1.4' not found (required by /usr/lib/liblapack.so.3gf)

When I run caller.sh from a separate shell (as in, I set a breakpoint, and copy-pasta the command inside system() into a terminal), everything runs fine. When I copy the innards of caller.sh straight into terminal, it runs fine.

But for some reason, Matlab system(['sh caller.sh']) causes it to blow up in the weirdest way, suggesting that the python library I'm calling in my own python script is missing some linear algebra library from Matlab's runtime?! Despite the fact that it runs independently of Matlab, and demonstrably so. I'm pretty confused.

Upvotes: 4

Views: 618

Answers (1)

wRAR
wRAR

Reputation: 25703

I suppose Matlab runs via a wrapper that sets LD_LIBRARY_PATH so that Matlab could load its bundled libraries from /usr/lib/matlab/R2012a/sys/os/glnxa64/. Of course that can and will break correct apps when they are run from matlab, because they were linked with incompatible versions of those libs. I can think only of unsetting LD_LIBRARY_PATH in your script but that is not the cleanest solution.

Upvotes: 1

Related Questions