Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError when calling python script via a shell script

Tags:

python

bash

shell

I've reduced the problem I am having to this. Here is the contents of my python script tmp.py:

    import numpy
    print "Imported numpy!"

If I call the python script directly at the command line

    $ python tmp.py

It successfully imports numpy and prints the print statement.

Here is the contents of my bash script test.sh:

    #!/bin/bash

    echo "PYTHONPATH:: $PYTHONPATH"
    echo "PATH:: $PATH"
    echo "LD_LIBRARY_PATH:: $LD_LIBRARY_PATH"
    pyver=`which python`
    echo "Using python version $pyver"
    python tmp.py

If I call this script at the command line,

    $ ./test.sh

I get the following error:

    Traceback (most recent call last):
      File "tmp.py", line 1, in <module>
        import numpy
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.py", line 148, in <module>
        import add_newdocs
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
        from numpy.lib import add_newdoc
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/__init__.py", line 13, in <module>
        from polynomial import *
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/polynomial.py", line 17, in <module>
        from numpy.linalg import eigvals, lstsq
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/__init__.py", line 48, in <module>
        from linalg import *
      File "/home/alex/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 23, in <module>
        from numpy.linalg import lapack_lite
    ImportError: libmkl_gf_lp64.so: cannot open shared object file: No such file or directory

I have checked that the results of echo $PYTHONPATH, echo $PATH, echo $LD_LIBRARY_PATH and which python all return the same whether called within the bash script or at the command line.

I have no idea what is going on!

like image 996
alexabate Avatar asked Sep 01 '26 17:09

alexabate


1 Answers

I had a similar problem. It turned out that in my bash script I was running the Python script with a different version of Python than I was on the command line (my env was set up with virtualenv on Python 2.7 but I was calling the script with Python3 from the bash script).

There is nothing in your snippets indicating this is the case but it is worth checking to make sure that the Python versions match up.

like image 78
Kelvin Avatar answered Sep 04 '26 07:09

Kelvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!