Douglas Su Douglas Su - 5 months ago 73
Python Question

how to set different PYTHONPATH variables for python3 and python2 respectively

I want to add a specific library path only to python2. After adding

export PYTHONPATH="/path/to/lib/"
to my
.bashrc
, however, executing python3 gets the error: Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!

I think it is due to that python2 and python3 share the common
PYTHONPATH
variable.

So, can I set different
PYTHONPATH
variables respectively for python2 and python3. If not, how can I add a library path exclusively to a particular version of python?

Answer

Create file mymodule.pth in python2 dist-packages path, (/usr/lib/python2.7/dist-packages) and add one line containing the directory to add to python path.
Do the same thing with python3.