Ubuntu 将目录添加到 Python 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16913086/
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 23:59:13  来源:igfitidea点击:

Ubuntu add directory to Python path

pythonubuntuimporterror

提问by PoGibas

I want to run third part tool written in python on my ubuntu machine (corgy tool).

我想在我的 ubuntu 机器上运行用 python 编写的第三方工具(corgy tool)。

However I don't know how to add additional modules to Python path.

但是我不知道如何向 Python 路径添加其他模块。

cat doc/download.rst         
There is currently no setup.py, so you need to manually add
the download directory to your PYTHON_PATH environment variable.

How can I add directory to PYTHON_PATH?

如何将目录添加到 PYTHON_PATH?

I have tried:
export PYTHON_PATH=/home/user/directory:$PYTHON_PATH && source .bashrc
export PATH=/home/user/directory:$PATH && source .bashrc

我试过了:
export PYTHON_PATH=/home/user/directory:$PYTHON_PATH && source .bashrc
export PATH=/home/user/directory:$PATH && source .bashrc

python
import sys
sys.path.append("/home/user/directory/")

python
import sys
sys.path.append("/home/user/directory/")

But when I try to run this tool I get:

但是当我尝试运行这个工具时,我得到:

Traceback (most recent call last):
File "examples/dotbracket_to_bulge_graph.py", line 4, in <module>
import corgy.graph.bulge_graph as cgb
ImportError: No module named corgy.graph.bulge_graph

采纳答案by fedorqui 'SO stop harming'

Create a .bash_profilein your home directory. Then, add the line

.bash_profile在您的主目录中创建一个。然后,添加行

PYTHONPATH=$PYTHONPATH:new_dir
EXPORT $PYTHONPATH

Or even better:

或者甚至更好:

if [ -d "new_dir" ] ; then
  PYTHONPATH="$PYTHONPATH:new_dir"
fi
EXPORT $PYTHONPATH

The .bash_profileproperties are loaded every time you log in.

.bash_profile特性被加载每次登录的时间。

The sourcecommand is useful if you don't want to log in again.

source如果您不想再次登录,该命令很有用。