51

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?

1

4 Answers 4

31

PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.

This could sound more tricky than it is, so let me show you how it works.

Let us assume your "package" has a single file named wow.py and you keep it in /home/user/mylib/wow.py.

Create the file /home/user/mylib/setup.py with the following content:

from setuptools import setup

setup(name="WowPackage",
      packages=["."],
)

That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:

  • "Really proper". Will copy your code to your python site-packages directory:

    $ python setup.py install
    
  • "Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.

    $ python setup.py develop
    
  • "User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.local directory, where Python will also find them on its own. For that, just add --user to the command.

    $ python setup.py install --user
    $ python setup.py develop --user
    

To remove a package installed in development mode, do

$ python setup.py develop -u

or

$ python setup.py develop -u --user

To remove a package installed "properly", do

 $ pip uninstall WowPackage

If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packages parameter of the setup function (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manual as well.

In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload (you'll need a PyPI username, though).

4
  • This is similar to the .pth file solution in that we're adding something directly into the default directories.
    – n.caillou
    Commented Jan 3, 2018 at 21:47
  • 1
    Of course, that's how it is implemented on a low level. The benefit is in the use of a convenient higher-level abstraction, namely that of a "package", along with standard command-line interface.
    – KT.
    Commented Jan 5, 2018 at 12:30
  • 1
    I was just commenting on the two solutions. Making the custom library an actual library/package is also a good solution, and it will be the more appropriate one in many situations. It is cleaner, though not as fast and flexible as the .pth one.
    – n.caillou
    Commented Jan 5, 2018 at 15:37
  • don't forget the __init__.py even though python3 doesn't need it, when creating this pkg - it won't be able to import it later ... (at least for me)
    – Ricky Levi
    Commented Jul 7, 2021 at 8:59
25

You can create a configuration file mymodule.pth under lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh), then add one line containing the directory to add to python path.

From docs.python2 and docs.python3:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

0
2

I found that there is no way to modify PYTHONPATH that is only for python2 or only for python3. I had to use a .pth file.

What I had to do was:

  • make sure directory is created in my home: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
  • create a .pth file in that directory
  • test that your .pth file is work
  • done

For more info on `.pth. file syntax and how they work please see: python2 docs and python3 docs.

(.pth files in a nutshell: when your python interpreter starts it will look in certain directories and see the .pth file, open those files, parse the files, and add those directories to your sys.path (i.e. the same behavior as PYTHONPATH) and make any python modules located on those directories available for normal importing.)

1
-1

If you don't want to bother with moving/adding documents in lib/site-packages, try adding two lines of code in the python2.7 script you would like to run (below.)

import sys
sys.path = [p for p in sys.path if p.startswith(r'C:\Python27')]

This way, PYTHONPATH will be updated (ignore all python3.x packages) every time you run your code.

1
  • 1
    The hard-coded Windows default path is not portable, and won't be correct even on some Windows installations. Maybe inspect sys.version instead.
    – tripleee
    Commented Feb 7, 2020 at 19:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.