3

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!

10
  • Are you running this script from the same directory like when you tested it from the command line? Commented Aug 23, 2013 at 17:47
  • Tested your approach and it works great for me using ubuntu. As stated in a previous comment, are tmp.py and test.sh in same directory? What does $ ls -Fal /bin/bash output? Commented Aug 23, 2013 at 17:48
  • 1
    Is your command-line actually bash or are you using a different shell? Does the output differ if you use $ . ./test.sh or $ source ./test.sh instead of $ ./test.sh? Commented Aug 23, 2013 at 17:48
  • Yes, the script is run in the same directory as tmp.py Commented Aug 23, 2013 at 17:49
  • 1
    And yes! The bash script works with $ source ./tesh.sh Why is this? Commented Aug 23, 2013 at 17:52

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.