使用 .pth 文件 [英] Using .pth files

查看:124
本文介绍了使用 .pth 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我没有全局 site-packages 目录的写访问权限的系统上发现一个模块,并且不改变环境 (PYTHONPATH>).我试图将 .pth 文件放在与我正在执行的脚本相同的目录中,但它似乎被忽略了.例如,我创建了一个文件 extras.pth ,内容如下:

N:\PythonExtras\lib\site-packages

但是以下脚本,放置并运行在同一目录中,打印 False.

导入系统在 sys.paths 中打印 r"N:\PythonExtras\lib\site-packages"

sys.path 中我有写访问权限的唯一目录是包含脚本的目录.是否有另一个(当前不存在的)目录可以放置 extras.pth 并让我看到它?有没有更好的方法来解决这个问题?

我在 Windows 上使用 python 2.7.我可以在这里找到的所有 .pth 问题都使用系统模块目录.

我已经在 %APPDATA%\Python\Python27\site-packages 找到了 Windows 每用户安装目录.我可以在那里放置一个模块,它将被导入,但是如果我在那里放置一个 .pth 文件,它就没有效果.这真的不应该工作,还是我做错了什么?

解决方案

文档中所述, PTH 文件只有在 site-packages 目录中时才会被处理.(更准确地说,如果它们位于站点目录"中,则会对其进行处理,但站点目录"本身是 Python 安装的全局设置,不依赖于当前目录或脚本所在的目录.)

如果包含您的脚本的目录在 sys.path 上,您可以在该目录中创建一个 sitecustomize.py.这将在 Python 启动时加载.在 sitecustomize.py 里面,你可以:

导入站点site.addsitedir('/some/dir/you/want/on/the/path')

这不仅会添加该目录,还会将其添加为站点目录",从而导致处理那里的 PTH 文件.如果您想创建自己的个人 site-packages-like-directory,这很方便.

如果您只需要在路径中添加一两个目录,则可以更简单地进行.只需创建一个用于操作 sys.path 的小型 Python 库,然后从您的脚本中导入该库.类似的东西:

# makepath.py导入系统sys.path.append('/whatever/dir/you/want')# 脚本.py导入生成路径

同样,根据文档,有可能%APPDATA%\Python\PythonXY\site-packages 中的站点特定目录(在 Windows 上).你可以试试这个,如果你实际上有写权限(而不仅仅是你的脚本目录).

I am trying to make a module discoverable on a system where I don't have write access to the global site-packages directory, and without changing the environment (PYTHONPATH). I have tried to place a .pth file in the same directory as a script I'm executing, but it seems to be ignored. E.g., I created a file extras.pth with the following content:

N:\PythonExtras\lib\site-packages

But the following script, placed and run in the same directory, prints False.

import sys
print r"N:\PythonExtras\lib\site-packages" in sys.paths

The only directory in sys.path to which I have write access is the directory containing the script. Is there another (currently non-existent) directory where I could place extras.pth and have it be seen? Is there a better way to go about this?

I'm using python 2.7 on Windows. All .pth questions I could find here use the system module directories.

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 .pth file there, it has no effect. Is this really not supposed to work, or am I doing something wrong?

解决方案

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.)

If the directory containing your script is on sys.path, you could create a sitecustomize.py in that directory. This will be loaded when Python starts up. Inside sitecustomize.py, you can do:

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.

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:

# 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).

这篇关于使用 .pth 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆