通过shell脚本调用python脚本时的ImportError

时间:2013-08-23 17:43:48

标签: python bash shell

我已经减少了我遇到的问题。这是我的python脚本tmp.py

的内容
    import numpy
    print "Imported numpy!"

如果我直接在命令行调用python脚本

    $ python tmp.py

它成功导入numpy并打印print语句。

以下是我的bash脚本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

如果我在命令行调用此脚本,

    $ ./test.sh

我收到以下错误:

    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

我已检查过echo $PYTHONPATHecho $PATHecho $LD_LIBRARY_PATHwhich python的结果是否都返回相同,无论是在bash脚本中还是在命令行中调用。< / p>

我不知道发生了什么事!

1 个答案:

答案 0 :(得分:3)

我有类似的问题。事实证明,在我的bash脚本中,我使用不同版本的Python运行Python脚本,而不是在命令行上运行(我的env是在Python 2.7上使用virtualenv设置的,但是我使用bash脚本中的Python3调用脚本)。

您的代码段中没有任何内容表明情况属实,但值得检查以确保Python版本匹配。