配置 Python 以使用站点包的其他位置

发布于 2024-12-11 20:35:55 字数 943 浏览 0 评论 0原文

有没有一种方法可以在不修改现有脚本的情况下告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔岸观火 2024-12-18 20:35:55

您可以使用站点特定配置挂钩

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

在您的情况下,您应该能够通过简单地放入包含要包含的目录路径的 .pth 文件来实现您想要的:

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

You can use the Site-specific configuration hook.

"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."

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
嘴硬脾气大 2024-12-18 20:35:55

您可以使用包装器脚本替换 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.

↘紸啶 2024-12-18 20:35:55

要更改Python的site-packages路径:

第1步:通过PYTHONUSERBASE环境变量

a 。对于 Windows

SET PYTHONUSERBASE=<custom-path>

For Example:
SET PYTHONUSERBASE=C:\\DEV\python_repo

第 2 步: 通过 --user

通过 pip 安装模块或包时,使用 < code>--user 如下所示:

python -m pip install --upgrade pip --user

注意:

  1. 按照上述步骤将站点包获取到自定义位置。

To change the path of site-packages of Python:

Step 1: via PYTHONUSERBASE environment variable

a. For Windows

SET PYTHONUSERBASE=<custom-path>

For Example:
SET PYTHONUSERBASE=C:\\DEV\python_repo

Step 2: via --user

While installing module or package via pip use --user as show below:

python -m pip install --upgrade pip --user

Note:

  1. Follow both the above step to get site-packages to custom location.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文