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

时间:2023-04-17
本文介绍了配置 Python 以使用站点包的其他位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有没有办法在不修改现有脚本的情况下告诉 Python 其他 site-packages 位置?

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

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

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

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

我知道我可以做到:

导入系统;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

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

解决方案

您可以使用 Site-具体配置钩子.

<块引用>

"路径配置文件是一个文件名,格式为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

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?

解决方案

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

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

上一篇:如何在不重复导入顶级名称的情况下构造python包 下一篇:python:不同包下同名的两个模块和类

相关文章