Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should pythonpath have directories specific to python2 and python3?

I am working with both python 2 and python 3 on a daily basis. Should I be adding paths to PYTHONPATH that are specific to a python version, like the following, which is specific to python 2?

/usr/local/lib/python2.7/dist-packages

If the answer is yes, then would python 3 know not to use these modules?

If the answer is no, then where should I add the above path if not to PYTHONPATH?

like image 597
Ofer Avatar asked Nov 02 '22 12:11

Ofer


1 Answers

Python 3 will attempt to load modules from your PYTHONPATH and fail. A possible solution to this is the following.

Set your PYTHONPATH to

/usr/local/lib/%PYTHON%/dist-packages

and create a file sitecustomize.py in the directory returned by

python -c "import site ; print site.getsitepackages()"

with the following content:

import sys
from distutils.sysconfig import get_python_version

sys.path = [x.replace('%PYTHON%', 'python{}'.format(get_python_version()))
            for x in sys.path]

This will replace the relevant part of the directory name with whatever python version is executed.

like image 111
Lars Avatar answered Nov 15 '22 05:11

Lars