Python 2.x issues with multiple versions regarding PYTHONPATH

Python 2.6 is installed on the system.

Now I want to use the modules introduced in Python 2.7. Since I do not have root privileges, I created and installed 2.7 under my home directory ($ HOME / local /)

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

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

Now I am faced with two problems that I want to ask for workarounds.

1. Call Python 2.7

The recently installed Python 2.7 does not find 2.6 modules in the system library path (/usr/lib/python2.6/site-packages/).

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

2. Call Python 2.6

Python 2.6 complains at startup:

'import site' failed; use -v for traceback

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

Thank.

+5
3

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)
...

, python 2.7 python2.7.

2) python 2.6

, , PYTHONHOME:

PYTHONHOME: Python. ${prefix}/lib/python [version] ${exec_prefix}/lib/python [version], ${prefix} ${exec_prefix} , , to/usr/local

python 2.7 . / python. PYTHONPATH ( PYTHONHOME).

. , Debian (, , ) python.

[: 1 niboshi.]

+3

Python PYTHONPATH sys.path. , sys.path

-:

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

Python 2.7 sys.path, Python 2.6 ( print sys.path ). , $HOME/local/lib/python2.7 .

, PYTHONPATH per script sys.path (sys.path.insert(0, '/home/user/local/lib/python2.7') script , .

. , collections Python 2.7 collections27.py , OrderedDict do from collection27 import OrderedDict

2.6 Python 2.6?

, . , Python 2.6 - :

export PYTHONPATH=$HOME/local/lib/python2.7:$PYTHONPATH
+2

May I suggest pythonbrew as a simpler alternative.

After installing pythonbrew:

$ pythonbrew install 2.7.2
$ pythonbrew switch 2.7.2
0
source

All Articles