1)调用python 2.7
简而言之:不要这样做。路径被称为“/usr/lib/python* 2.6 */site-packages/”是有原因的。
一个原因是,在此目录中通常存储“已编译”的 python 文件 (.pyc)。python 2.6 和 python 2.7 .pyc 文件不兼容:
$ python2.7 /usr/lib/python2.6/sitecustomize.pyc
RuntimeError: Bad magic number in .pyc file
python 将跳过它无法理解的 pyc 文件,但您至少会失去预编译文件的好处。
另一个原因是,事情可能会混淆:
$ 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 所需的模块。
2)调用python 2.6
您可能想查看描述 PYTHONHOME 的手册页部分:
PYTHONHOME:更改标准 Python 库的位置。默认情况下,在 ${prefix}/lib/python[version] 和 ${exec_prefix}/lib/python[version] 中搜索库,其中 ${prefix} 和 ${exec_prefix} 是安装相关目录,均默认到 /usr/local
您可以将 python 2.7 特定文件/模块存储在本地安装的相应目录中。这些文件/模块只会在您运行特定版本的 python 时被拾取。在这种情况下,您不得设置 PYTHONPATH(或 PYTHONHOME)。
注意:这正是 Debian(可能还有其他发行版)管理不同同时安装的 python 版本的方式。
[编辑:在收到 niboshi 的评论后添加了第 1 节。]