The variable PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. This variable is not set by default and not needed for Python to work because it it already knows where to find its standard libraries (sys.path).

But if for any reason you need some custom Python libraries that you do not want to install, you can export PYTHONPATH=/path/to/my/modules/ to add /path/to/my/modules/ to the paths that Python will explore, then it will know where to find those custom modules.

And this time, if you print again sys.path, it will display you the newly added modules.

Answer from kurisukun on Stack Overflow
Discussions

Usage of PYTHONPATH
I am the beginner. I am still confused with the use of PYTHONPATH. The results of my research are as follows: I create the environement key ‘PYTHONPATH’ with the following values: C:\Program Files\Python\DLLs C:\Program Files\Python\Lib C:\Program Files\Python C:\Program Files\Python\L... More on discuss.python.org
🌐 discuss.python.org
7
0
August 9, 2022
What is Python's site-packages directory? - Stack Overflow
On my CentOS 7.9 Linux (a Red Hat clone) it is found in ~/.local/lib/python3.9/site-packages/ and there isn't any need to include it in the PYTHONPATH variable. ... I upvoted, unfortunately no other answer gives any hint for per-user python modules. I needed to change path from python3.9 -> ... More on stackoverflow.com
🌐 stackoverflow.com
suse - Configure Python to include another directory when looking for packages - Unix & Linux Stack Exchange
How do I configure Python so that ...python2.6/site-packages? ... I guess that PYTHONPATH could do the job. Look in "6.1.2. The Module Search Path". ... I don't want to force everyone to have to PYTHONPATH defined. Isn't there a way to change the default? ... lorin, you can either set an environment variable (like PYTHONHOME), ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Configuring Python to use additional locations for site-packages - Stack Overflow
Is there a way to tell Python about additional site-packages locations without modifying existing scripts? On my CentOS 5.5 server I have a Python 2.7 installation that is installed in /opt/python... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
1184

There are two types of site-packages directories, global and per user.

  1. Global site-packages ("dist-packages") directories are listed in sys.path when you run:

     python -m site
    

    For a more concise list run getsitepackages from the site module in Python code:

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

    Caution: In virtual environments getsitepackages is not available with older versions of virtualenv, sys.path from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:

     python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
    
  2. The per user site-packages directory (PEP 370) is where Python installs your local packages:

     python -m site --user-site
    

    If this points to a non-existing directory check the exit status of Python and see python -m site --help for explanations.

    Hint: Running pip list --user or pip freeze --user gives you a list of all installed per user site-packages.


Practical Tips

  • <package>.__path__ lets you identify the location(s) of a specific package: (details)

      $ python -c "import setuptools as _; print(_.__path__)"
      ['/usr/lib/python2.7/dist-packages/setuptools']
    
  • <module>.__file__ lets you identify the location of a specific module: (difference)

      $ python3 -c "import os as _; print(_.__file__)"
      /usr/lib/python3.6/os.py
    
  • Run pip show <package> to show Debian-style package information:

      $ pip show pytest
      Name: pytest
      Version: 3.8.2
      Summary: pytest: simple powerful testing with Python
      Home-page: https://docs.pytest.org/en/latest/
      Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
      Author-email: None
      License: MIT license
      Location: /home/peter/.local/lib/python3.4/site-packages
      Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
    
2 of 16
707
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

(or just first item with site.getsitepackages()[0])

🌐
Leemendelowitz
leemendelowitz.github.io › blog › how-does-python-find-packages.html
How does python find packages? // Lee On Coding // My blog about coding and stuff.
Using Ubuntu's Python, my packages were installed to /usr/local/lib/python2.7/dist-packages, whereas the new Python I installed expects packages to be installed to /usr/local/lib/python2.7/site-packages. I just had to manipulate the PYTHONPATH environment variable to point to dist-packages in order to gain access to the previously installed packaged with the newly installed version of Python. So how does the Ubuntu distribution of Python know to use /usr/local/lib/python2.7/dist-packages in sys.path?
🌐
Python Module of the Week
pymotw.com › 2 › site
site – Site-wide configuration - Python Module of the Week
$ python site_user_base.py Base: /Users/dhellmann/.local Site: /Users/dhellmann/.local/lib/python2.7/site-packages · The user base directory can be set through the PYTHONUSERBASE environment variable, and has platform-specific defaults (~/Python$version/site-packages for Windows and ~/.local ...
🌐
Python
docs.python.org › 3 › library › sys_path_init.html
The initialization of the sys.path module search path — Python 3.15.2 documentation
The PYTHONUSERBASE environment variable controls where is searched for user site-packages and the PYTHONNOUSERSITE environment variable prevents searching for user site-packages all together.
🌐
Python.org
discuss.python.org › python help
Usage of PYTHONPATH - Python Help - Discussions on Python.org
August 9, 2022 - The results of my research are as follows: I create the environement key ‘PYTHONPATH’ with the following values: C:\Program Files\Python\DLLs C:\Program Files\Python\Lib C:\Program Files\Python C:\Program Files\Python\Lib\site-packages ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › What-is-PYTHONPATH-environment-variable-in-Python
What is PYTHONPATH environment variable in Python?
August 26, 2023 - This is particularly useful for maintaining project-specific libraries or when you don't have permissions to install packages globally. PYTHONPATH is an environment variable that extends Python's module search path.
Top answer
1 of 2
6

(Please correct errors and omissions as necessary. Thanks.)

First, a question and a comment. I don't use SUSE, so take this with a pinch of salt. Are the packages that install in /usr/lib/python2.6/site-packages official packages? If so, SUSE is broken, so that is not likely. If they are not official packages, you could either ask the packagers to use the standard paths, or, alternatively, you could submit a wishlist bug to SUSE asking them to support this additional path. This will save you and other people additional headaches.

For the moment, you have the following possibilities, in order of decreasing scope:

  1. Change the module search path for all users (method 1)

    Change the module search path in the Python installation.

    The default module search path is hardwired into the binary. Add-on paths can be configured at runtime, for example in the site.py file. For example, Debian uses /usr/lib/python2.6/site.py (for the default python 2.6 installation) to do its site-specific configuration. At the top of the file is written

    Append module search paths for third-party packages to `sys.path`.
    
    ****************************************************************
    * This module is automatically imported during initialization. *
    ****************************************************************
    [...]
    
    This will append site-specific paths to the module search path.
    

    The Debian patch debian/patches/site-locations.diff says

    For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used.

    The patch in question is

                    sitedirs.append(os.path.join(prefix,
    "Lib", "site-packages"))
                 elif os.sep == '/':
        +            sitedirs.append(os.path.join(prefix,
    "local/lib",
        +                                        "python" + sys.version[:3],
        +                                        "dist-packages"))
                     sitedirs.append(os.path.join(prefix,
    "lib",
                                                 "python" + sys.version[:3],
        -                                        "site-packages"))
        -            sitedirs.append(os.path.join(prefix,
    "lib", "site-python"))
        +                                        "dist-packages"))
        +            sitedirs.append(os.path.join(prefix,
    "lib", "dist-python"))
                 else:
                     sitedirs.append(prefix)
                     sitedirs.append(os.path.join(prefix,
    "lib", "site-packages"))
    

    So you could modify the site.path in your system package to produce a modified module search path. You probably don't want to this, though. For one thing, you will have to merge this in on every update of your distribution's python package.

  2. Change the module search path for all users (method 2)

    Add a file of the form something.pth to a directory that is already in the search path, which contains a path, either relative or absolute. Eg.

    /usr/local/lib/python2.6/dist-packages$ cat foo.pth 
    /home/faheem/dummypythonpath
    

    In another terminal do

    >>> import sys
    >>> sys.path
        [...,'/home/faheem/dummypythonpath',...,]
    
  3. Change the module search path for all users (method 3)

    The environment variable PYTHONPATH is normally used to append to the system path at user level. You can put it in a file which will be sourced by all users. Eg. in Debian we have /etc/bash.bashrc, which says at the top

    # System-wide .bashrc file for interactive bash(1) shells.
    
    # To enable the settings / commands in this file for login shells as well,
    # this file has to be sourced in /etc/profile.
    

    So you could add or PYTHONPATH there. You probably want it to be sourced for both login and interactive shells, so you'll want to check on that. Unfortunately, distributions are often flaky about enabling this.

    The paths in PYTHONPATH are added to the default list of search paths in the system (which can be obtained for example by sys.path - see below). Allowing for the possibility that PYTHONPATH is set already, just add desired additional directories to it, eg.

    export PYTHONPATH=$PYTHONPATH:"/home/username/lib/python2.6/dist-packages"
    

    If you source the PYTHONPATH variable, and then check sys.path again, you will
    see the paths have been added. Note that the position in which the paths in
    PYTHONPATH are added to the pre-existing paths does not seem to be prescribed by
    the implementation.

  4. Change the module search path per user.

    The usual way is to change PYTHONPATH in the user's bashrc, namely ~/.bashrc. Again, check that it is sourced for both login and interactive shells.

  5. Change the module search path on a per script basis. This is done by appending to sys.path, namely

    import sys
    sys.path.append(some_additional_path)
    

    This will only work for the script that is importing this. This is normally used, as far as I know, for casual use, when importing modules in nonstandard locations, like from somewhere in a home directory.

See also Greg Ward on Modifying Python's Search Path. This has a good discussion of the available alternatives.

2 of 2
3

Use sys.path:

import sys
sys.path.append("/home/me/mypy")

You can also check the site module documentation, which explains how the site-specific paths are computed.

🌐
GeeksforGeeks
geeksforgeeks.org › python › pythonpath-environment-variable-in-python
PYTHONPATH Environment Variable in Python - GeeksforGeeks
January 9, 2026 - PYTHONPATH is an environment variable that tells Python where to look for modules and packages beyond the standard library and installed site-packages.
🌐
Python
docs.python.org › 3 › library › site.html
site — Site-specific configuration hook — Python 3.15.1 ...
2 weeks ago - Changed in version 3.3: Importing the module used to trigger paths manipulation even when using -S. It starts by constructing up to four directories from a head and a tail part. For the head part, it uses sys.prefix and sys.exec_prefix; empty heads are skipped. For the tail part, it uses the empty string and then lib/site-packages (on Windows) or lib/pythonX.Y[t]/site-packages (on Unix and macOS).
🌐
Readthedocs
the-hitchhikers-guide-to-packaging.readthedocs.io › en › latest › introduction.html
Introduction to Packaging — The Hitchhiker's Guide to Packaging 1.0 documentation
The PYTHONPATH variable can be set to a list of paths that will be added to the beginning of sys.path. For example, if PYTHONPATH is set to /www/python:/opt/py, the search path will begin with ['', '/www/python', '/opt/py', ...]. ... Directories must exist in order to be added to sys.path.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python and the module search path
Python and the Module Search Path | Towards Data Science
January 18, 2025 - python also creates the sys.path variable based on the above formula, specifying to find packages in that corresponding site-packages folder ... The above works great if we want to set up an environment with a specific version of python and the associated dependencies which we can grab from PyPI.
🌐
Baeldung
baeldung.com › home › scripting › augmenting the default search path for python
Augmenting the Default Search Path for Python | Baeldung on Linux
March 18, 2024 - The framework generates a shared library that contains the exposed classes and functions. This library is visible to Python. Besides the Python modules, we must also copy this shared library to one of the site-packages directories. We can expand the module search path using the PYTHONPATH environment variable.
🌐
Clawpack
clawpack.org › v5.6.x › python_path.html
Python path — Clawpack 5.6.1 documentation
More generally, to find ... in Python: ... this will tell you where the users’ site-packages directory is. If you installed using the –user flag in the pip install, then it is the easy-install.pth in this directory that contains the path. If you installed without the –user flag, then then system-wide site-packages/easy-install.pth file has been modified. You can find the path to this via: ... Setting the environment variable PYTHONPATH ...