5

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?

1
  • 2
    I suggest you to user virtualenv and virtualenvwrapper instead of changing PYTHONPATH manuallly Commented Oct 28, 2013 at 8:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Use parenthesis around the print function for Python 3 python -c "import site ; print(site.getsitepackages())"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.