在 shell 中更改 PYTHONPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15315384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:46:18  来源:igfitidea点击:

Changing PYTHONPATH in shell

pythonpythonpath

提问by user2152303

I'm a completely new user to Python and shell scripts, and have run into a dead end with this, even after Googling the issue and banging my head against the desk a lot. Any help is appreciated!

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

I'm running Python 2.7.3 on a shell that I SSH into; I downloaded some code to run a few programs/analyses. When I execute the initial program, I get the following error:

我在通过 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

[Script 1] refers to a python script in the same folder that came as part of the code package, and it also calls the Google Data python package, which I've downloaded to the same folder and have gunzipped, tar unpacked, and then installed (with ./configure, etc.) Based on looking up the errors, my best guess is that there's something wrong with the PYTHONPATH here, and it's not finding [script1].py and the Gdata folder, even though both are within the same directory as the script I'm running. "Echo $PYTHONPATH" tells me that it's an undefined variable, and there's also a blank init.py file within the directory. There's no files containing the word "bash" or "bashrc" anywhere within that directory. Likewise, I can't seem to find any "sys.path" files, although when I boot up Python and print(sys.path) I get the resulting output:

[Script 1] 指的是作为代码包的一部分出现在同一个文件夹中的一个 python 脚本,它也调用了 Google Data python 包,我已经将它下载到同一个文件夹并进行了压缩、tar 解压,然后安装(使用 ./configure 等)基于查找错误,我最好的猜测是这里的 PYTHONPATH 有问题,并且它没有找到 [script1].py 和 Gdata 文件夹,即使两者都在与我正在运行的脚本相同的目录。“Echo $PYTHONPATH”告诉我这是一个未定义的变量,还有一个空白的init.py 文件在目录中。该目录中的任何位置都没有包含“bash”或“bashrc”一词的文件。同样,我似乎找不到任何“sys.path”文件,但当我启动 Python 并打印(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']

I've also tried

我也试过

export PYTHONPATH=[directory]

in my shell, but it spits out "export: command not found".

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

Please forgive a newcomer to all this - any help on this (whether or not my suspicions are correct, and how to resolve them) would be greatly appreciated!

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

采纳答案by miikkas

Setting PYTHONPATH

设置 PYTHONPATH

By the output of the exportcommand you tried, it looks like the shell you are using is not bash. This postcovers 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.

根据export您尝试的命令的输出,您使用的 shell 似乎不是 bash。这篇文章介绍了一些有关如何找出您正在使用的 shell 的方法。找到您的 shell 后,您可以了解如何在该 shell 中设置环境变量 (PYTHONPATH)。

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(最后一个应该适用于 (T)CSH):

PYTHONPATH=your_directory python script_name

and

env PYTHONPATH=your_directory python script_name

Testing that the PYTHONPATH you set works

测试您设置的 PYTHONPATH 是否有效

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.

要查看 PYTHONPATH 是否真的在 Python 中设置并运行python script_name,请使用python -c 'import os; print os.getenv("PYTHONPATH")'. 它应该显示您刚刚设置的 PYTHONPATH。

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

同样,sys.path在 Python 解释器中打印应该将 PYTHONPATH 中的路径作为条目之一输出。

If PYTHONPATH is set correctly

如果 PYTHONPATH 设置正确

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

如果您成功设置了 PYTHONPATH 并且问题仍然存在,请尝试从gdata所在的路径运行 Python 解释器。

cd path_which_has_subdirectory_gdata
python

In Python interpreter, try importing the gdata module:

在 Python 解释器中,尝试导入 gdata 模块:

import gdata

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

如果可行,请尝试导入导致以下问题的模块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__.pyand PYTHONPATH should be set to point to the directory abovethe module in hierarchy.

如果这些导入在 Python 解释器中起作用,那么您的 [script1] 可能有问题。如果不是,请尝试确认 gdata 模块确实在您认为的位置;模块的正确目录应包含一个名为的文件,__init__.py并且 PYTHONPATH 应设置为指向层次结构中模块上方的目录。