关于 PYTHONPATH 的 Python 2.x 多版本问题 [英] Python 2.x multiple version issues regarding PYTHONPATH

查看:47
本文介绍了关于 PYTHONPATH 的 Python 2.x 多版本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统中安装了 Python 2.6.

There's Python 2.6 installed in the system.

现在我想使用 Python 2.7 中引入的模块.因为我没有root权限,所以我建了&在我的主目录下安装了 2.7 ($HOME/local/)

Now I want to use modules introduced in Python 2.7. Because I have no root privilege, I have built & installed 2.7 under my home directory ($HOME/local/)

我在 $HOME/.bashrc 中添加了以下内容:

I added the following to my $HOME/.bashrc:

export PATH=$HOME/local/bin:$PATH
export PYTHONPATH=$HOME/local/lib/python2.7:$PYTHONPATH

现在我遇到了我想寻求解决方法的两个问题.

Now I encountered the two problems I want ask for workarounds.

新安装的 Python 2.7 在系统库路径 (/usr/lib/python2.6/site-packages/) 中找不到 2.6 模块.

Newly installed Python 2.7 doesn't find 2.6 modules in system's library path (/usr/lib/python2.6/site-packages/).

我应该手动将它添加到 PYTHONPATH 吗?有没有更好的解决方案?

Should I add it to PYTHONPATH manually? Is there any nicer solution?

Python 2.6 启动时报错:

Python 2.6 complains at startup:

'import site' failed; use -v for traceback

我猜它正在尝试加载 2.7 模块(在 $HOME/local/lib/python2.7 中).是否可以在调用 Python 2.6 时只加载 2.6 模块?

I guess it's trying to load 2.7 modules (in $HOME/local/lib/python2.7). Is it possible to load only 2.6 modules when Python 2.6 is invoked?

谢谢.

推荐答案

1) 调用 python 2.7

简而言之:不要这样做.路径被称为'/usr/lib/python*2.6*/site-packages/'是有原因的.

1) Invoking python 2.7

In short: don't do this. There are reasons why the path is called '/usr/lib/python*2.6*/site-packages/'.

一个原因是,在这个目录中通常存储编译"的python文件(.pyc).python 2.6 和 python 2.7 .pyc 文件不兼容:

One reason is, that in this directory typically the 'compiled' python files (.pyc) are stored. python 2.6 and python 2.7 .pyc files are not compatible:

$ python2.7 /usr/lib/python2.6/sitecustomize.pyc
RuntimeError: Bad magic number in .pyc file

python 会跳过它无法理解的 pyc 文件,但您至少会失去预编译文件的好处.

python will skip pyc files which it cannot understood, but you loose at least the benefits of precompiled files.

另一个原因是,事情可能会混淆:

Another reason is, that things might get mixed up:

$ strace -f python2.7 /usr/lib/python2.6/sitecustomize.py
...
stat("/etc/python2.6", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/etc/python2.6", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/etc/python2.6/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hookmodule.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.py", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/python2.6/apport_python_hook.pyc", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib/python2.7/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hookmodule.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.py", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/lib/python2.7/apport_python_hook.pyc", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib/python2.7/plat-linux2/apport_python_hook", 0x7fffa15601f0) = -1 ENOENT (No such file or directory)
...

在你的情况下,我会在 python2.7 目录中安装 python 2.7 所需的模块.

I would in your case install the modules needed also for python 2.7 in the python2.7 directory.

您可能想查看手册页中描述 PYTHONHOME 的部分:

You might want to have a look at the part of the man page where PYTHONHOME is described:

PYTHONHOME:更改标准 Python 库的位置.默认情况下,在 ${prefix}/lib/python[version] 和 ${exec_prefix}/lib/python[version] 中搜索库,其中 ${prefix} 和 ${exec_prefix} 是安装相关目录,均默认到/usr/local

PYTHONHOME: Change the location of the standard Python libraries. By default, the libraries are searched in ${prefix}/lib/python[version] and ${exec_prefix}/lib/python[version], where ${prefix} and ${exec_prefix} are installation-dependent directories, both defaulting to /usr/local

您可以将 python 2.7 特定文件/模块存储在本地安装的相应目录中.只有当您运行特定版本的 python 时,才会选取这些文件/模块.在这种情况下,您不得设置 PYTHONPATH(或 PYTHONHOME).

You can store the python 2.7 specific files / modules in the appropriate directory in your local installation. Those files / modules will only be picked up when you run the specific version of python. In this case you must not set the PYTHONPATH (or PYTHONHOME).

注意:这正是 Debian(可能还有其他发行版)管理不同的同时安装的 Python 版本的方式.

Note: this is exactly the way Debian (and maybe other distributions) manage different simultaneously installed versions of python.

这篇关于关于 PYTHONPATH 的 Python 2.x 多版本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆