将目录添加到 sys.path /PYTHONPATH

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

adding directory to sys.path /PYTHONPATH

pythonmechanizepython-importpythonpath

提问by UnadulteratedImagination

I am trying to import a module from a particular directory.

我正在尝试从特定目录导入模块。

The problem is that if I use sys.path.append(mod_directory)to append the path and then open the python interpreter, the directory mod_directorygets added to the end of the list sys.path. If I export the PYTHONPATHvariable before opening the python interpreter, the directory gets added to the start of the list. In the latter case I can import the module but in the former, I cannot.

问题是,如果我使用sys.path.append(mod_directory)附加路径然后打开 python 解释器,该目录mod_directory将被添加到列表 sys.path 的末尾。如果我PYTHONPATH在打开 python 解释器之前导出变量,该目录将被添加到列表的开头。在后一种情况下,我可以导入模块,但在前一种情况下,我不能。

Can somebody explain why this is happening and give me a solution to add the mod_directoryto the start, insidea python script ?

有人可以解释为什么会发生这种情况,并给我一个解决方案mod_directorypython 脚本中将其添加到开头吗?

采纳答案by Ned Deily

This is working as documented. Any paths specified in PYTHONPATHare documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append()appends to the existing path. See hereand here. If you want a particular directory to come first, simply insert it at the head of sys.path:

这是按照文档工作的。中指定的任何路径PYTHONPATH都记录为通常在工作目录之后但在标准解释器提供的路径之前。 sys.path.append()附加到现有路径。请参阅此处此处。如果您希望某个特定目录首先出现,只需将其插入 sys.path 的开头即可:

import sys
sys.path.insert(0,'/path/to/mod_directory')

That said, there are usually better ways to manage imports than either using PYTHONPATHor manipulating sys.pathdirectly. See, for example, the answers to this question.

也就是说,通常有比直接使用PYTHONPATH或操作更好的方法来管理导入sys.path。例如,请参阅此问题的答案。

回答by Δημητρη? Παππ??

You could use:

你可以使用:

import os
path = 'the path you want'
os.environ['PATH'] += ':'+path

回答by user3517603

Temporarily changing dirs works well for importing:

临时更改目录适用于导入:

cwd = os.getcwd()
os.chdir(<module_path>)
import <module>
os.chdir(cwd)

回答by Alexander Pacha

When running a Python script from Powershell under Windows, this should work:

在 Windows 下从 Powershell 运行 Python 脚本时,这应该可以工作:

$pathToSourceRoot = "C:/Users/Steve/YourCode"
$env:PYTHONPATH = "$($pathToSourceRoot);$($pathToSourceRoot)/subdirs_if_required"

# Now run the actual script
python your_script.py

回答by Jayhello

As to me, i need to caffe to my python path. I can add it's path to the file /home/xy/.bashrcby add

至于我,我需要caffe到我的python路径。我可以将它的路径添加到该文件 /home/xy/.bashrc由add

export PYTHONPATH=/home/xy/caffe-master/python:$PYTHONPATH.

export PYTHONPATH=/home/xy/caffe-master/python:$PYTHONPATH.

to my /home/xy/.bashrcfile.

到我的/home/xy/.bashrc文件。

But when I use pycharm, the path is still not in.

但是当我使用pycharm时,路径仍然不在。

So I can add path to PYTHONPATHvariable, by run -> edit Configuration.

所以我可以PYTHONPATH通过运行 -> 编辑配置来添加变量的路径。

enter image description here

在此处输入图片说明