You can use the Site-specific configuration hook.

"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."

In your case, you should be able to achieve what you want by simply dropping in a .pth file containing the path of the directory to include:

[root@home]$ echo "/usr/local/lib/python2.7/site-packages/" > /opt/python2.7.2/lib/python2.7/site-packages/usrlocal.pth
Answer from Shawn Chin on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ site.html
site โ€” Site-specific configuration hook โ€” Python 3.15.2 documentation
2 weeks ago - To explicitly trigger the usual site-specific additions, call the main() function. 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).
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])

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.

๐ŸŒ
Python
svn.python.org โ€บ projects โ€บ python โ€บ trunk โ€บ Lib โ€บ site.py
site.py
Assume foo.pth contains the following: # foo package configuration foo bar bletch and bar.pth contains: # bar package configuration bar Then the following directories are added to sys.path, in this order: /usr/local/lib/python2.5/site-packages/bar /usr/local/lib/python2.5/site-packages/foo Note that bletch is omitted because it doesn't exist; bar precedes foo because bar.pth comes alphabetically before foo.pth; and spam is omitted because it is not mentioned in either path configuration file.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ get-location-of-python-site-packages-directory
Get Location of Python site-packages Directory - GeeksforGeeks
July 23, 2025 - This could be compiled into a one-liner using the -c attribute present in the Python command, which executes the code given in the argument. py -c "import site; print(site.getsitepackages())" ... This gives a list containing the path of ...
๐ŸŒ
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 - We can then add the following to it: > cd /Users/jamisonm/.pyenv/versions/3.9.7/lib/python3.9/site-packages > echo "/Users/jamisonm/test" > test.pth ยท where the last command is a one liner to create a text file with the directory that we want. Now if we start python and run sys.path we see the following:
๐ŸŒ
Readthedocs
the-hitchhikers-guide-to-packaging.readthedocs.io โ€บ en โ€บ latest โ€บ introduction.html
Introduction to Packaging โ€” The Hitchhiker's Guide to Packaging 1.0 documentation
TODO Better define where the .../site-packages/ directory is located. The most convenient way is to add a path configuration file to a directory thatโ€™s already in Pythonโ€™s path, which could be the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how do i find the location of my python site-packages directory?
How do I find the location of my Python site-packages directory? | Codemia
December 28, 2024 - The fastest way to find your Python site-packages directory is to run python -m site or python -c "import site; print(site.getsitepackages())". The site-packages directory is where pip installs third-party packages for a specific Python interpreter, and its location varies depending on the operating system, the Python version, and whether you are using a virtual environment, Conda, or the system interpreter. The critical detail is that site-packages is not a machine-wide directory. It belongs to a specific interpreter installation. If you check the wrong interpreter, you get the wrong path, even if the commands themselves are correct.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-do-I-find-the-location-of-my-Python-site-packages-directory
How do I find the location of my Python site-packages directory?
May 15, 2025 - You can also find site-packages directories by running this command in your terminal. It prints all known site directories for the active Python interpreter ? ... sys.path = [ 'C:\current\directory', 'C:\Python312\python312.zip', 'C:\Python312\DLLs', 'C:\Python312\Lib', 'C:\Python312', 'C:\Python312\Lib\site-packages', ] USER_BASE: 'C:\Users\username\AppData\Roaming\Python' USER_SITE: 'C:\Users\username\AppData\Roaming\Python\Python312\site-packages' ENABLE_USER_SITE: True
๐ŸŒ
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 - For example, /usr/lib64/python3.6/site-packages is one of them. When we download third-party Python libraries, these libraries are installed in those directories. We can use path configuration files for augmenting the search paths. A path configuration file is a file that contains additional paths to be added to sys.path.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to find the location of Python site-packages directory - YouTube
How to find the location of Python site-packages directory
Published: May 11, 2019
๐ŸŒ
Python Packaging
packaging.python.org โ€บ tutorials โ€บ installing-packages
Installing Packages - Python Packaging User Guide
(Starting in version 10, pip displays ... to your PATH: On Linux and macOS you can find the user base binary directory by running python -m site --user-base and adding bin to the end....