问题描述
是否可以在不修改现有脚本的情况下告诉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?
推荐答案
您可以使用网站-特定配置挂钩.
对于您而言,只需放入包含目录路径的.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以将其他位置用于站点包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!