将目录永久添加到 PYTHONPATH?

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

Permanently add a directory to PYTHONPATH?

pythonwindowspythonpath

提问by John Howard

Whenever I use sys.path.append, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH?

每当我使用时sys.path.append,都会添加新目录。但是,一旦我关闭 python,列表将恢复到以前的(默认?)值。如何将目录永久添加到PYTHONPATH

采纳答案by Alex Martelli

You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profileor whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.

您需要将新目录添加到环境变量中PYTHONPATH,并用冒号与其先前的内容分隔。在任何形式的 Unix 中,您都可以在适合您正在使用的任何 shell(.profile或其他任何 shell ,取决于您最喜欢的 shell)的启动脚本中使用命令执行此操作,该命令同样取决于相关的 shell;在 Windows 中,您可以通过系统 GUI 来实现。

superuser.commay be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.

superuser.com如果您需要有关如何在您选择的平台和 shell 中丰富环境变量的详细信息,可能是一个更好的地方进一步询问,即更多详细信息,因为它本身并不是一个真正的编程问题。

回答by awesomo

If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc

如果您使用 bash(在 Mac 或 GNU/Linux 发行版上),请将其添加到您的 ~/.bashrc

export PYTHONPATH="${PYTHONPATH}:/my/other/path"

回答by Blue Peppers

You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.

您可以通过 pythonrc 文件添加路径,该文件在 Linux 上默认为 ~/.pythonrc。IE。

import sys
sys.path.append('/path/to/dir')

You could also set the PYTHONPATHenvironment variable, in a global rc file, such ~/.profileon mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.

您还可以PYTHONPATH在全局 rc 文件中设置环境变量,例如~/.profile在 mac 或 linux 上,或通过控制面板 -> 系统 -> 高级选项卡 -> Windows 上的环境变量。

回答by janex

Just to add on awesomo's answer, you can also add that line into your ~/.bash_profileor ~/.profile

只是为了添加 awesomo 的答案,您还可以将该行添加到您的~/.bash_profile~/.profile

回答by sth

Instead of manipulating PYTHONPATHyou can also create a path configuration file. First find out in which directory Python searches for this information:

PYTHONPATH您还可以创建路径配置文件,而不是进行操作。首先找出Python在哪个目录中搜索此信息:

python -m site --user-site

For some reason this doesn't seem to work in Python 2.7. There you can use:

出于某种原因,这在 Python 2.7 中似乎不起作用。在那里你可以使用:

python -c 'import site; site._script()' --user-site

Then create a .pthfile in that directory containing the path you want to add (create the directory if it doesn't exist).

然后.pth在该目录中创建一个包含您要添加的路径的文件(如果该目录不存在,则创建该目录)。

For example:

例如:

# 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"

回答by entrepaul

In case anyone is still confused - if you are on a Mac, do the following:

如果有人仍然感到困惑 - 如果您使用的是 Mac,请执行以下操作:

  1. Open up Terminal
  2. Type open .bash_profile
  3. In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
  4. Save the file, restart the Terminal, and you're done
  1. 打开终端
  2. 类型 open .bash_profile
  3. 在弹出的文本文件中,在最后添加这一行: export PYTHONPATH=$PYTHONPATH:foo/bar
  4. 保存文件,重新启动终端,你就完成了

回答by Zah

On linux you can create a symbolic link from your package to a directory of the PYTHONPATH without having to deal with the environment variables. Something like:

在 linux 上,您可以创建从包到 PYTHONPATH 目录的符号链接,而无需处理环境变量。就像是:

ln -s /your/path /usr/lib/pymodules/python2.7/

回答by Peter Piper

For me it worked when I changed the .bash_profilefile. Just changing .bashrcfile worked only till I restarted the shell.

对我来说,当我更改.bash_profile文件时它起作用了。只是更改.bashrc文件才起作用,直到我重新启动外壳。

For python 2.7 it should look like:

对于 python 2.7,它应该如下所示:

export PYTHONPATH="$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"

at the end of the .bash_profilefile.

.bash_profile文件的末尾。

回答by auserdude

This works on Windows

这适用于 Windows

  1. On Windows, with Python 2.7 go to the Python setup folder.
  2. Open Lib/site-packages.
  3. Add an example.pth empty file to this folder.
  4. Add the required path to the file, one per each line.
  1. 在 Windows 上,使用 Python 2.7 转到 Python 安装文件夹。
  2. 打开 Lib/site-packages。
  3. 将 example.pth 空文件添加到此文件夹。
  4. 添加所需的文件路径,每行一个。

Then you'll be able to see all modules within those paths from your scripts.

然后,您将能够从脚本中看到这些路径中的所有模块。

回答by Pierz

To give a bit more explanation, Python will automatically construct its search paths (as mentioned aboveand here) using the site.pyscript (typically located in sys.prefix + lib/python<version>/site-packagesas well as lib/site-python). One can obtain the value of sys.prefix:

为了给出更多解释,Python 将使用脚本(通常位于 sys.prefix +以及 中)自动构建其搜索路径(如上所述此处)。可以获取 sys.prefix 的值:site.pylib/python<version>/site-packageslib/site-python

python -c 'import sys; print(sys.prefix)'

The site.py script then adds a number of directories, dependent upon the platform, such as /usr/{lib,share}/python<version>/dist-packages, /usr/local/lib/python<version>/dist-packagesto the search path and alsosearches these paths for <package>.pthconfig files which contain specific additional search paths. For example easy-installmaintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it's /usr/local/lib/python2.7/dist-packages/easy-install.pth. On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path:

然后,site.py 脚本会根据平台将许多目录(例如/usr/{lib,share}/python<version>/dist-packages)添加/usr/local/lib/python<version>/dist-packages到搜索路径中,并且还会在这些路径中<package>.pth搜索包含特定附加搜索路径的配置文件。例如,easy-install维护其已安装包的集合,这些包被添加到系统特定的文件中,例如在 Ubuntu 上它是/usr/local/lib/python2.7/dist-packages/easy-install.pth. 在典型的系统上,有一堆这样的 .pth 文件可以解释 sys.path 中的一些意外路径:

python -c 'import sys; print(sys.path)'

So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH.

因此可以创建一个 .pth 文件并将其放入这些目录中的任何一个(包括上面提到的 sitedir )。这似乎是大多数包添加到 sys.path 的方式,而不是使用 PYTHONPATH。

Note: On OSX there's a special additional search path added by site.py for 'framework builds' (but seems to work for normal command line use of python): /Library/Python/<version>/site-packages(e.g. for Python2.7: /Library/Python/2.7/site-packages/) which is where 3rd party packages are supposed to be installed (see the README in that dir). So one can add a path configuration file in there containing additional search paths e.g. create a file called /Library/Python/2.7/site-packages/pip-usr-local.pthwhich contains /usr/local/lib/python2.7/site-packages/and then the system python will add that search path.

注意:在 OSX 上,site.py 为“框架构建”添加了一个特殊的附加搜索路径(但似乎适用于 Python 的正常命令行使用):(/Library/Python/<version>/site-packages例如,对于 Python2.7 : /Library/Python/2.7/site-packages/)这是第 3 方软件包的位置要安装(请参阅该目录中的自述文件)。因此,可以在其中添加一个包含其他搜索路径的路径配置文件,例如创建一个名为/Library/Python/2.7/site-packages/pip-usr-local.pth其中包含的文件/usr/local/lib/python2.7/site-packages/,然后系统 python 将添加该搜索路径。