How to set PYTHONPATH differently for version 2 and 3?

You can set different sys.path for Python 2 and Python 3 using path configuration (.pth) files.

For instance, to add a directory to sys.path for Python 2, create a .pth file in any of Python 2 site-packages directories (i.e. returned by site.getsitepackages() or site.getusersitepackages()):

Python 2.7.11 (default, Dec  6 2015, 15:43:46) 
[GCC 5.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()
['/usr/lib/python2.7/site-packages', '/usr/lib/site-python']

Then create a .pth file (as root):

echo "/ver2packages" > /usr/lib/python2.7/site-packages/ver2packages.pth

See site module documentation for more.


For Linux, you can create a symbolic link to you library folder and place it in your aimed version:

ln -s /your/path /usr/local/lib/python3.6/site-packages

This is not about changing PYTHONPATH but an alternative solution.

Tags:

Python