配置Python以将其他位置用于站点包 [英] Configuring Python to use additional locations for site-packages

查看:65
本文介绍了配置Python以将其他位置用于站点包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不修改现有脚本的情况下告诉Python其他site-packages位置?

Is there a way to tell Python about additional site-packages locations without modifying existing scripts?

在CentOS 5.5服务器上,我在/opt/python2.7.2中安装了Python 2.7安装,在/opt/python2.7.2/lib/python2.7/site-packages中有一个site-packages文件夹.

On my CentOS 5.5 server I have a Python 2.7 installation that is installed in /opt/python2.7.2 and there is a site-packages folder at /opt/python2.7.2/lib/python2.7/site-packages.

这样做的原因是,我不想打扰5.5发行版随附的现有Python 2.4安装.

The reason for this is that I didn't want to disturb the existing Python 2.4 install that shipped with the 5.5 distribution.

但是,第三方Python应用程序还在/usr/local/lib/python2.7/site-packages处添加了site-packages文件夹,并将其自身安装在该位置.

However a third party Python application also added a site-packages folder at: /usr/local/lib/python2.7/site-packages and installed itself at that location.

部分原因是我的错,因为在安装之前我没有在应用程序的Makefile中调整PREFIX,但是现在我对此无能为力.

This is partly my fault because I didn't tweak the PREFIX in the application's Makefile before installing, however there's not much I can do about it now.

我知道我可以做到:

import sys; sys.path.insert(0, "/usr/local/lib/python2.7/site-packages")

但是,这将涉及到我要跟踪每个脚本并添加上面的内容,如果将来有更新,这是不理想的.

However it would involve me tracking down every script and adding the above which is not ideal should there be updates in the future.

要解决此问题,我在/opt/python2.7.2/lib/python2.7/site-packages中创建了一个指向此第三方应用程序位置的符号链接:

To get around this I created a symbolic link in /opt/python2.7.2/lib/python2.7/site-packages to the location of this third party application:


ln -sf /usr/local/lib/python2.7/site-packages/theapp /opt/python2.7.2/lib/python2.7/site-packages/theapp

这似乎工作正常,但我想知道是否有更好的方法?

This appears to work fine but I'm wondering if there's a better way?

推荐答案

您可以使用网站-特定配置挂钩.

路径配置文件是名称为name.pth的文件,并且存在于上述四个目录之一中;其内容是要添加到sys.path的其他项(每行一个).

"A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path."

对于您而言,只需放入包含目录路径的.pth文件即可,从而实现您想要的目标:

In your case, you should be able to achieve what you want by simply dropping in a .pth file containing the path of the directory to include:

[root@home]$ echo "/usr/local/lib/python2.7/site-packages/" > /opt/python2.7.2/lib/python2.7/site-packages/usrlocal.pth

这篇关于配置Python以将其他位置用于站点包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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