python - 永久添加目录到PYTHONPATH?
每当我使用sys.path.append时,都会添加新目录。 但是,一旦我关闭python,列表将恢复到之前的(默认?)值。 如何将目录永久添加到PYTHONPATH?
14个解决方案
352 votes
如果您正在使用bash(在Mac或GNU / Linux发行版上),请将其添加到您的~/.bashrc
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
awesomo answered 2019-02-23T09:23:13Z
106 votes
您需要将新目录添加到环境变量superuser.com,并用以前内容的冒号分隔。 在任何形式的Unix中,您都可以在适合您正在使用的任何shell(.profile或其他任何shell,取决于您最喜欢的shell)的启动脚本中使用命令执行此操作,该命令同样取决于所讨论的shell; 在Windows中,您可以通过系统GUI执行此操作。
superuser.com可能是一个更好的地方进一步询问,即如果您需要有关如何在您选择的平台和shell中丰富环境变量的详细信息,更多细节,因为它本身并不是一个真正的编程问题。
Alex Martelli answered 2019-02-23T09:22:31Z
73 votes
您也可以创建路径配置文件,而不是操作.pth。 首先找出Python搜索此信息的目录:
python -m site --user-site
出于某种原因,这似乎在Python 2.7中不起作用。 在那里你可以使用:
python -c 'import site; site._script()' --user-site
然后在该目录中创建一个包含要添加的路径的.pth文件(如果该目录不存在,则创建该目录)。
例如:
# find directory
SITEDIR=$(python -m site --user-site)
# create if it doesn't exist
mkdir -p "$SITEDIR"
# create new .pth file with our path
echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth"
sth answered 2019-02-23T09:24:07Z
24 votes
这适用于Windows
在Windows上,使用Python 2.7转到Python安装文件夹。
打开Lib / site-packages。
将example.pth空文件添加到此文件夹。
添加文件所需的路径,每行一个。
然后,您将能够从脚本中查看这些路径中的所有模块。
auserdude answered 2019-02-23T09:25:12Z
19 votes
万一有人仍然感到困惑 - 如果你在Mac上,请执行以下操作:
打开终端
型号export PYTHONPATH=$PYTHONPATH:foo/bar
在弹出的文本文件中,在末尾添加以下行:export PYTHONPATH=$PYTHONPATH:foo/bar
保存文件,重新启动终端,然后就完成了
entrepaul answered 2019-02-23T09:26:17Z
18 votes
您可以通过pythonrc文件添加路径,默认为linux上的〜/ .pythonrc。即。
import sys
sys.path.append('/path/to/dir')
您还可以在全局rc文件中设置PYTHONPATH环境变量,例如在mac或linux上的~/.profile,或通过控制面板 - > 系统 - > 高级标签 - > Windows上的环境变量。
Blue Peppers answered 2019-02-23T09:26:54Z
10 votes
为了给出更多解释,Python将使用/Library/Python//site-packages脚本(通常位于sys.prefix + /Library/Python/2.7/site-packages/以及/Library/Python/2.7/site-packages/pip-usr-local.pth)自动构建其搜索路径(如上所述和此处所述)。 可以获取sys.prefix的值:
python -c 'import sys; print(sys.prefix)'
然后,site.py脚本会根据平台(例如/Library/Python//site-packages,/Library/Python/2.7/site-packages/)向搜索路径添加许多目录,并在这些路径中搜索包含特定其他搜索路径的/Library/Python/2.7/site-packages/pip-usr-local.pth配置文件。 例如,easy-install维护其已安装软件包的集合,这些软件包被添加到系统特定文件中,例如在Ubuntu /usr/local/lib/python2.7/site-packages/上。在典型系统上,有一堆这些.pth文件可以解释sys.path中的一些意外路径:
python -c 'import sys; print(sys.path)'
因此,可以创建.pth文件并放入任何这些目录(包括上面提到的sitedir)。 这似乎是大多数软件包添加到sys.path的方式,而不是使用PYTHONPATH。
注意:在OSX上有一个特殊的附加搜索路径由site.py为'框架构建'添加(但似乎适用于python的正常命令行使用):/Library/Python//site-packages(例如对于Python2.7:/Library/Python/2.7/site-packages/)这是第三方软件包 应该安装(参见该目录中的自述文件)。 因此,可以在其中添加包含其他搜索路径的路径配置文件,例如 创建一个名为/Library/Python/2.7/site-packages/pip-usr-local.pth的文件,其中包含/usr/local/lib/python2.7/site-packages/,然后系统python将添加该搜索路径。
Pierz answered 2019-02-23T09:27:50Z
9 votes
在linux上,您可以创建从包到PYTHONPATH目录的符号链接,而无需处理环境变量。 就像是:
ln -s /your/path /usr/lib/pymodules/python2.7/
Zah answered 2019-02-23T09:28:22Z
9 votes
对我来说,当我更改.bash_profile文件时,它有效。 只更改.bashrc文件只能重新启动shell。
对于python 2.7,它应该看起来像:
export PYTHONPATH="$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"
在.bash_profile文件的末尾。
Peter Piper answered 2019-02-23T09:29:06Z
5 votes
只需添加awesomo的答案,您也可以将该行添加到您的~/.bash_profile或~/.profile
janex answered 2019-02-23T09:29:41Z
4 votes
如果PYTHONPATH当前不存在(由于:),则将〜/ .bashrc添加export PYTHONPATH="${PYTHONPATH}:/my/other/path"可能不起作用。
export PYTHONPATH="/my/other/path1"
export PYTHONPATH="${PYTHONPATH}:/my/other/path2"
在Ubuntu 16.04上将上面的内容添加到我的〜/ .bashrc中
boyangeor answered 2019-02-23T09:30:18Z
0 votes
我在Windows Vista,Python 3.5中永久添加
系统> 控制面板> 高级系统设置> 高级(点击)环境变量> (如果在变量列中没有看到PYTHONPATH)(单击)新建> 变量名称:PYTHONPATH> 变量值:
请在Variable值中写下目录。 这是Blue Peppers答案的细节。
Cloud Cho answered 2019-02-23T09:31:11Z
0 votes
在Python 3.6.4中,你可以在python会话中持久化sys.path,如下所示:
import sys
import os
print(str(sys.path))
dir_path = os.path.dirname(os.path.realpath(__file__))
print(f"current working dir: {dir_path}")
root_dir = dir_path.replace("/util", '', 1)
print(f"root dir: {root_dir}")
sys.path.insert(0, root_dir)
print(str(sys.path))
我强烈建议你使用virtualenv和virtualenvwrapper,否则你会混乱你的道路
Rubber Duck answered 2019-02-23T09:31:50Z
0 votes
下面的脚本适用于所有平台,因为它是纯Python。 它利用pathlib Path,在此处记录[https://docs.python.org/3/library/pathlib.html],使其跨平台工作。 你运行一次,重新启动内核就是这样。 灵感来自[https://medium.com/@arnaud.bertrand/modifying-python-s-search-path-with-pth-files-2a41a4143574。]
from pathlib import Path
to_add=Path(path_of_directory_to_add)
from sys import path
if str(to_add) not in path:
minLen=999999
for index,directory in enumerate(path):
if 'site-packages' in directory and len(directory)<=minLen:
minLen=len(directory)
stpi=index
pathSitePckgs=Path(path[stpi])
with open(str(pathSitePckgs/'current_machine_paths.pth'),'w') as pth_file:
pth_file.write(str(to_add))
andrei deusteanu answered 2019-02-23T09:32:23Z