配置 Python 以使用站点包的其他位置
有没有一种方法可以在不修改现有脚本的情况下告诉 Python 有关其他 site-packages
位置的信息?
在我的 CentOS 5.5 服务器上,我在 /opt/python2.7.2
中安装了 Python 2.7,并且在 /opt/ 处有一个
。site-packages
文件夹python2.7.2/lib/python2.7/site-packages
这样做的原因是我不想打扰 5.5 发行版附带的现有 Python 2.4 安装。
但是,第三方 Python 应用程序还在以下位置添加了一个 site-packages
文件夹:/usr/local/lib/python2.7/site-packages
并将其自身安装在该位置。
这部分是我的错,因为在安装之前我没有调整应用程序的 Makefile
中的 PREFIX
,但现在我对此无能为力。
我知道我可以这样做:
import sys; sys.path.insert(0, "/usr/local/lib/python2.7/site-packages")
但是,这将涉及我跟踪每个脚本并添加上述内容,如果将来有更新,这并不理想。
为了解决这个问题,我在 /opt/python2.7.2/lib/python2.7/site-packages
中创建了一个指向此第三方应用程序位置的符号链接:
ln -sf /usr/local/lib/python2.7/site-packages/theapp /opt/python2.7.2/lib/python2.7/site-packages/theapp
这似乎工作正常,但我'我想知道是否有更好的方法?
Is there a way to tell Python about additional site-packages
locations without modifying existing scripts?
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
.
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.
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.
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.
I know that I can do this:
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用站点特定配置挂钩。
在您的情况下,您应该能够通过简单地放入包含要包含的目录路径的
.pth
文件来实现您想要的:You can use the Site-specific configuration hook.
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:您可以使用包装器脚本替换 python 可执行文件,该包装器脚本将添加的安装路径附加到 PYTHONPATH。但这是一个拼凑。
但我会尝试修复附加组件的安装,以便它正确进入站点包目录。
You could replace the python executable with a wrapper script which appends your added installpath to PYTHONPATH. But this is a kludge.
But I'd try to fix the installation of the add-on so that it properly goes into the site-packages dir.
要更改Python的
site-packages
路径:第1步:通过
PYTHONUSERBASE
环境变量a 。对于 Windows
第 2 步: 通过
--user
通过
pip
安装模块或包时,使用 < code>--user 如下所示:注意:
To change the path of
site-packages
of Python:Step 1: via
PYTHONUSERBASE
environment variablea. For Windows
Step 2: via
--user
While installing module or package via
pip
use--user
as show below:Note: