7

我是 Python 和 shell 脚本的全新用户,即使在谷歌搜索这个问题并将我的头撞在桌子上之后,我也遇到了死胡同。任何帮助表示赞赏!

我在通过 SSH 连接的 shell 上运行 Python 2.7.3;我下载了一些代码来运行一些程序/分析。当我执行初始程序时,出现以下错误:

    Traceback (most recent call last):
    File "./[script1].py", line 7, in <module>
    import [script1]
    File "[directory]/[script].py", line 22, in <module>
    import gdata.spreadsheet.service
    ImportError: No module named gdata.spreadsheet.service

[脚本 1] 是指作为代码包的一部分出现在同一文件夹中的 python 脚本,它还调用了 Google Data python 包,我已将其下载到同一文件夹并进行了 gunzip、tar 解压,然后已安装(使用 ./configure 等)基于查找错误,我最好的猜测是这里的 PYTHONPATH 有问题,并且它没有找到 [script1].py 和 Gdata 文件夹,即使两者都在与我正在运行的脚本相同的目录。“Echo $PYTHONPATH”告诉我这是一个未定义的变量,还有一个空白的初始化.py 目录中的文件。该目录中的任何地方都没有包含单词“bash”或“bashrc”的文件。同样,我似乎找不到任何“sys.path”文件,尽管当我启动 Python 和 print(sys.path) 时,我得到了结果输出:

['', 
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

我也试过

export PYTHONPATH=[directory]

在我的外壳中,但它会吐出“导出:找不到命令”。

请原谅所有这一切的新手-对此的任何帮助(无论我的怀疑是否正确,以及如何解决它们)将不胜感激!

4

1 回答 1

8

Setting PYTHONPATH

By the output of the export command you tried, it looks like the shell you are using is not bash. This post covers some ways on how to find out which shell you are on. After finding out your shell, you can find out how to set environment variables (PYTHONPATH) in that shell.

You might also try these to set the PYTHONPATH for the duration of running your script (the last one should work on (T)CSH):

PYTHONPATH=your_directory python script_name

and

env PYTHONPATH=your_directory python script_name

Testing that the PYTHONPATH you set works

To see that PYTHONPATH really gets set and works within Python, instead of running the script like above with python script_name, use python -c 'import os; print os.getenv("PYTHONPATH")'. It should display the PYTHONPATH you just set.

Likewise, printing sys.path in Python interpreter should output the path in PYTHONPATH as one of the entries.

If PYTHONPATH is set correctly

If you successfully set your PYTHONPATH and the problem persists, try running the Python interpreter from the path you have gdata in.

cd path_which_has_subdirectory_gdata
python

In Python interpreter, try importing the gdata module:

import gdata

If that works, try also importing the module that causes the ImportError:

import gdata.spreadsheet.service

If these imports work from Python interpreter, there's probably something wrong with your [script1]. If not, try to confirm that gdata module really is where you think it is; the correct directory for the module should contain a file named __init__.py and PYTHONPATH should be set to point to the directory above the module in hierarchy.

于 2013-03-09T23:16:32.603 回答