Python 使用 .pth 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15208615/
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
Using .pth files
提问by alexis
I am trying to make a module discoverable on a system where I don't have write access to the global site-packagesdirectory, and without changing the environment (PYTHONPATH). I have tried to place a .pthfile in the same directory as a script I'm executing, but it seems to be ignored. E.g., I created a file extras.pthwith the following content:
我试图在我没有对全局site-packages目录的写访问权限的系统上发现一个模块,并且不改变环境 ( PYTHONPATH)。我试图将一个.pth文件放在与我正在执行的脚本相同的目录中,但它似乎被忽略了。例如,我创建了一个extras.pth包含以下内容的文件:
N:\PythonExtras\lib\site-packages
But the following script, placed and run in the same directory, prints False.
但是以下脚本,放置并运行在同一目录中,打印False.
import sys
print r"N:\PythonExtras\lib\site-packages" in sys.paths
The only directory in sys.pathto which I have write access is the directory containing the script. Is there another (currently non-existent) directory where I could place extras.pthand have it be seen? Is there a better way to go about this?
sys.path我拥有写访问权限的唯一目录是包含脚本的目录。是否有另一个(当前不存在的)目录可以放置extras.pth并显示它?有没有更好的方法来解决这个问题?
I'm using python 2.7 on Windows. All .pthquestions I could find here use the system module directories.
我在 Windows 上使用 python 2.7。.pth我在这里找到的所有问题都使用系统模块目录。
Edit:I've tracked down the Windows per-user installation directory, at %APPDATA%\Python\Python27\site-packages. I can place a module there and it will be imported, but if I put a .pthfile there, it has no effect. Is this really not supposed to work, or am I doing something wrong?
编辑:我已经在%APPDATA%\Python\Python27\site-packages. 我可以在那里放置一个模块,它将被导入,但是如果我在.pth那里放置一个文件,它就没有效果。这真的不应该工作,还是我做错了什么?
采纳答案by BrenBarn
As described in the documentation, PTH files are only processed if they are in the site-packages directory. (More precisely, they are processed if they are in a "site directory", but "site directory" itself is a setting global to the Python installation and does not depend on the current directory or the directory where the script resides.)
如文档中所述,只有在 site-packages 目录中时才会处理 PTH 文件。(更准确地说,如果它们位于“站点目录”中,则会对其进行处理,但“站点目录”本身是 Python 安装的全局设置,不依赖于当前目录或脚本所在的目录。)
If the directory containing your script is on sys.path, you could create a sitecustomize.pyin that directory. This will be loaded when Python starts up. Inside sitecustomize.py, you can do:
如果包含您的脚本的目录在 上sys.path,您可以sitecustomize.py在该目录中创建一个。这将在 Python 启动时加载。在里面sitecustomize.py,你可以这样做:
import site
site.addsitedir('/some/dir/you/want/on/the/path')
This will not only add that directory, but will add it as a "site directory", causing PTH files there to be processed. This is handy if you want to create your own personal site-packages-like-directory.
这不仅会添加该目录,还会将其添加为“站点目录”,从而导致处理那里的 PTH 文件。如果您想创建自己的个人site-packages类目录,这很方便。
If you only need to add one or two directories to the path, you could do so more simply. Just create a tiny Python library that manipulates sys.path, and then import that library from your script. Something like:
如果您只需要向路径添加一两个目录,则可以更简单地进行。只需创建一个用于操作 的小型 Python 库,sys.path然后从您的脚本中导入该库。就像是:
# makepath.py
import sys
sys.path.append('/whatever/dir/you/want')
# script.py
import makepath
Edit: Again, according to the documentation, there is the possibility of a site-specific directory in %APPDATA%\Python\PythonXY\site-packages(on Windows). You could try that, if in fact you have write access to that (and not just to your script directory).
编辑:同样,根据文档,%APPDATA%\Python\PythonXY\site-packages(在 Windows 上)可能存在特定于站点的目录。你可以试试这个,如果你实际上有写访问权限(而不仅仅是你的脚本目录)。
回答by Wool
You can make a .pth (path) file in a directory already in sys.path so it can be included/
您可以在 sys.path 中已有的目录中创建 .pth(路径)文件,以便将其包含/

