What does my PYTHONPATH contain?

20,767

Typically, the environment variable $PYTHONPATH is empty (try echo $PYTHONPATH). The actual list of folders python searches for libraries can be found with (in python):

import sys
print(sys.path)

This will consist (in search order) of the current directory, any directories in your $PYTHONPATH, and finally the default library directories, set by site.py. The main default locations are (where X.Y is the python version, eg 2.7):

/usr/lib/pythonX.Y (python system libraries, eg re, urllib)
/usr/lib/pythonX.Y/dist-packages (python libraries installed with deb packages)

If you use pip install --user or similar to install libraries as yourself, it will also contain the user library directory:

/home/USERNAME/.local/lib/pythonX.Y/site-packages

You can manipulate the PYTHONPATH by either setting the environment variable before you launch python (PYTHONPATH=$PYTHONPATH:/foo/bar), or by editing sys.path once you've started python (import sys; sys.path = ["/foo/bar"] + sys.path).

However, if you want to play with your own python libraries, a good idea is to create a virtualenv. This is a directory in which you can play around with your own versions of python libraries without any risk of messing up the python libraries used by the system. See How to set up and use a virtual python environment in Ubuntu? for information about creating and using a virtualenv.

Share:
20,767

Related videos on Youtube

Niccolò
Author by

Niccolò

Updated on September 18, 2022

Comments

  • Niccolò
    Niccolò over 1 year

    I work on some personal python libraries and I need to display what PYTHONPATH contains and then manipulate it.

  • Alex
    Alex over 8 years
    Thanks. Studying Programming Python 4th Edition by O'Reilly books, and it constantly refers to $PYTHONPATH as an OS variable found with os.environ. Spent way too long looking for an object I'm not even going to change.