简体   繁体   English

假设无法访问site-packages目录,使用Python .pth文件

[英]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.pyusercustomize.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.pypaths.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注释掉exportsPYTHONPATH ,并获得一个新的终端,我看到了:

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.

相关问题 创建辅助站点包目录(并从其中的.pth文件加载包) - Creating a secondary site-packages directory (and loading packages from .pth files therein) 删除站点包中的 .pth 文件后证书验证失败 - certificate verify failed after deleting .pth files in site-packages “/lib/python2.7/site-packages/不支持.pth文件”在MacOS上安装pip - “/lib/python2.7/site-packages/ does NOT support .pth files” installing pip on MacOS 使用CMake查找python site-packages目录 - Finding python site-packages directory with CMake python的站点包目录是什么? - What is python's site-packages directory? zope.deprectation:解决pydev,.pth文件和站点包的导入问题 - zope.deprectation: solve import issues with pydev, .pth files and site-packages Errno 13 权限被拒绝:&#39;/Library/Python/2.7/site-packages/test-easy-install-18954.pth&#39; - Errno 13 Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-18954.pth' 安装python settuptools时发生错误-没有这样的文件或目录:&#39;/usr/local/lib/python2.7/site-packages/test-easy-install-8811.pth&#39; - Error when installing python settuptools - No such file or directory: '/usr/local/lib/python2.7/site-packages/test-easy-install-8811.pth' Python中的站点包 - site-packages in Python 位于 venv 站点包目录中的 Cythonize python 包 - Cythonize python packages located in venv site-packages directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM