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

Configuring Python to use additional locations for site-packages(配置 Python 以使用站点包的其他位置)
本文介绍了配置 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 以使用站点包的其他位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)