[英]Using Python .pth files assuming no access to site-packages directory
In my current system, all directories such as .local/lib/python2.7/site-packages
are under global admin control and are standardized across users. 在我当前的系统中,所有目录(如
.local/lib/python2.7/site-packages
都受全局管理控制,并且跨用户标准化。
However, I would like to add a collection of my own directories to be brought into the PYTHONPATH on startup, preferably in an easy-to-manage way like using a .pth
file within a secondary site directory. 但是,我想添加一个我自己的目录集合,在启动时加入PYTHONPATH,最好是以一种易于管理的方式,比如在辅助站点目录中使用
.pth
文件。
Is there a way I can achieve this through normal Python channels, but without needing to make any kind of change to the standard locations where Python looks for path elements? 有没有办法通过普通的Python通道实现这一点,但是不需要对Python寻找路径元素的标准位置进行任何改变?
I'm specifically wondering if there is a way to just place a file like sitecustomize.py
or usercustomize.py
anywhere in a folder that is in the PYTHONPATH and expect that they will be automatically executed? 我特别想知道是否有办法只将
sitecustomize.py
或usercustomize.py
这样的文件放在PYTHONPATH文件夹中的任何位置 ,并期望它们会自动执行? (Rather than getting this behavior by placing such files within an admin directory like site-packages
, which is what I want to avoid.) (而不是通过将这些文件放在像
site-packages
这样的管理目录中来获得这种行为,这是我想要避免的。)
My goal is to place a text file, paths.pth
in some directory, like /home/my_configs/python_stuff/
and then just need to place that directory only in the PYTHONPATH via, eg a .bashrc
file. 我的目标是将一个文本文件
paths.pth
放在某个目录中,比如/home/my_configs/python_stuff/
然后只需要将该目录放在PYTHONPATH中,例如.bashrc
文件。 Then, at least, I only manually maintain one path to be added, and it's easier to version control or parameterize what gets loaded by changing what is in that .pth
file. 然后,至少,我只手动维护一个要添加的路径,并且通过更改该
.pth
文件中的内容,更容易进行版本控制或参数化加载的内容。
If the admin limitations weren't in place, I could trivially do this in site-packages
but I need a solution that works totally outside of that. 如果管理员限制没有到位,我可以在
site-packages
轻松地做到这一点,但我需要一个完全不在其中的解决方案。
In /.bashrc
I have this: 在
/.bashrc
我有这个:
export PYTHONPATH=/home/ems/python_paths/:$PYTHONPATH
In the directory /home/ems/python_paths/
I have just two files, sitecustomize.py
and paths.pth
(but I've also tried adding an __init__.py
there too, which (as expected) didn't help). 在目录
/home/ems/python_paths/
我只有两个文件, sitecustomize.py
和paths.pth
(但我也尝试过添加一个__init__.py
,这(如预期的那样)没有帮助)。 These two files look like this: 这两个文件看起来像这样:
sitecustomize.py sitecustomize.py
import site
site.addsitedir("/home/ems/python_paths/")
paths.pth paths.pth
/home/ems/ws/Research/Projects/python-util/src/util/
/home/ems/ws/Research/Projects/python-efficacy/src/efficacy/
The contents of paths.pth
are just exactly what used to be directly exported in .bashrc
and, for instance, permits me to do from util.DataManager import DataManager
as a top-level import. paths.pth
的内容正是以前在.bashrc
直接导出的内容,例如,允许我from util.DataManager import DataManager
作为顶级导入。
But after setting up the various files above, commenting out exports
to PYTHONPATH
in .bashrc
, and getting a fresh terminal, I see this: 但是在设置了上面的各种文件之后,在
.bashrc
注释掉exports
到PYTHONPATH
,并获得一个新的终端,我看到了:
ems@computer ~ $ python
Enthought Python Distribution -- www.enthought.com
Version: 7.3-2 (64-bit)
Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 11 2012, 17:52:16)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "credits", "demo" or "enthought" for more information.
Hello
>>> from util.DataManager import DataManager
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named util.DataManager
There was PEP 370 specifically addressing the creation of per-user site-packages
directories, to deal with the situation where the user has no admin access to the system-wide site-packages
. PEP 370专门针对每用户
site-packages
目录的创建,以处理用户无权访问系统范围的site-packages
。
For example on Unix (including Mac OS), and assuming one is using Python 3.6, one can create the following directory and place .pth
files inside there 例如在Unix(包括Mac OS)上,假设有人使用Python 3.6,可以创建以下目录并在其中放置
.pth
文件
~/.local/lib/python3.6/site-packages
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.