PYTHONPATH - 不同Python安装的动态?

发布于 2024-12-08 04:50:30 字数 1192 浏览 0 评论 0原文

在我的 CentOS5 服务器上,我同时拥有 Python2.4 和 2.6(yum 工作需要 2.4)。我不确定发生了什么,但每次我尝试运行一个其模块加载到 2.4 site-packages 目录中的文件时,系统突然变得非常混乱。我检查了 PYTHONPATH/sys.path,看起来所有内容都被 2.6 环境数据覆盖了。

以前没有这样做过。我只是在脚本开头的 shebang 语句中声明了 /usr/bin/python 或 /usr/bin/python26 ,它总是能找到正确的模块。

有没有办法让 PYTHONPATH 变量是动态的并根据正在运行的 python 解释器加载不同的路径?

否则,我将不得不手动编辑每个应用程序中的路径,这似乎有点矫枉过正。

它是在安装 web.py (顺便说一句,我喜欢它)之后开始的。

回溯: 正如下面有人评论的那样,我将这个程序的 shebang 更改为 #!/usr/bin/env python:

Traceback (most recent call last):
  File "/usr/bin/linkchecker", line 24, in ?
    import codecs
  File "/usr/lib/python2.6/codecs.py", line 268
    return (b"", 0)
              ^

另一个例子,尝试使用 yum:

Traceback (most recent call last):
  File "/usr/bin/yum", line 5, in ?
    import yum
  File "/usr/lib/python2.4/site-packages/yum/__init__.py", line 21, in ?
    import os
  File "/usr/lib/python2.6/os.py", line 758
    bs = b""
           ^

我注意到有几个程序不会被 b"" 语法混淆,并且所有其中一些本来要使用 2.4 的程序由于某种原因而使用 2.6。如果我尝试让程序使用 2.6 解释器,它能够理解该语法,但无法找到任何其他模块(位于 2.4 site-packages 目录中)。

我不知道该语法是什么,因为它们被写入我从 sourceforge 获得的模块中,但它们上周正在工作。我不确定发生了什么变化。

谢谢, 汤姆

On my CentOS5 server, I have both Python2.4 and 2.6 (2.4 is required for yum to work). I'm not sure what happened, but suddenly the system gets very confused every time I try to run a file whose modules are loaded into the 2.4 site-packages directory. I checked the PYTHONPATH/sys.path and it looks like everything was overwritten with 2.6 environment data instead.

It didn't used to do this. I simply declared /usr/bin/python or /usr/bin/python26 in the shebang statement at the beginning of the script and it always found the correct modules just fine.

Is there a way for the PYTHONPATH variable to be dynamic and load different paths based on which python interpreter is running?

Otherwise I'm going to have to manually edit the path in every application, which seems like overkill.

It started after installing web.py (which I love, by the way).

Traceback:
As someone commented below, I changed the shebang to be #!/usr/bin/env python for this program:

Traceback (most recent call last):
  File "/usr/bin/linkchecker", line 24, in ?
    import codecs
  File "/usr/lib/python2.6/codecs.py", line 268
    return (b"", 0)
              ^

Another example, trying to use yum:

Traceback (most recent call last):
  File "/usr/bin/yum", line 5, in ?
    import yum
  File "/usr/lib/python2.4/site-packages/yum/__init__.py", line 21, in ?
    import os
  File "/usr/lib/python2.6/os.py", line 758
    bs = b""
           ^

I've noticed a couple programs not confounded by the b"" syntax, and all of them are programs meant to use 2.4 that are for some reason using 2.6. If I try to make the program use the 2.6 interpreter it is able to understand that syntax, but then can't find any of the other modules (which are in the 2.4 site-packages directory).

I don't know what that syntax is, as they were written into modules which I got from sourceforge, however they were working last week. I am not sure what changed.

Thanks,
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ζ澈沫 2024-12-15 04:50:30

首先,使用 virtualenv 来隔离多个 Python 安装的包。你的大部分问题都会立即消失。

其次,正如 Ibp 在他的回答中建议的那样,更改 shebang 行以使用“当前活动的”python 二进制文件,以便它可以跨多个解释器工作。

First of all, use virtualenv to isolate packages for multiple Python installations. Most of your problems will go away immediately.

Second, as Ibp has recommended in his answer, change the shebang line to use the "currently active" python binary so that it will work across multiple interpreters.

梦中楼上月下 2024-12-15 04:50:30

不要使用 shebang (文件的第一个字节),而是

#!/usr/bin/python

使用 shebang

#!/usr/bin/env python

编辑:我同意 Noufal 使用 virtualenv 的建议。

Instead of using the shebang (first bytes of the file)

#!/usr/bin/python

use the shebang

#!/usr/bin/env python

Edit: I second Noufal's suggestion to use virtualenv.

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