将目录添加到sys.path / PYTHONPATH

问题:将目录添加到sys.path / PYTHONPATH

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

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

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

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_directory gets added to the end of the list sys.path. If I export the PYTHONPATH variable 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.

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


回答 0

如文档所述,这正在工作。PYTHONPATH文件中指定的所有路径通常在工作目录之后但在标准解释器提供的路径之前记录。 sys.path.append()追加到现有路径。看到这里这里。如果要让特定目录优先出现,只需将其插入sys.path的开头即可:

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

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

This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of 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 PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.


回答 1

您可以使用:

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

You could use:

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

回答 2

对于我来说,我需要了解我的python路径。我可以将它的路径添加到该文件 /home/xy/.bashrc由add

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

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

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

因此,我可以PYTHONPATH通过运行->编辑配置将路径添加到变量。

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

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

to my /home/xy/.bashrc file.

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

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


回答 3

临时更改目录可以很好地导入:

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

Temporarily changing dirs works well for importing:

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

回答 4

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

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

# Now run the actual script
python your_script.py

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

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

# Now run the actual script
python your_script.py