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
Discussions

suse - Configure Python to include another directory when looking for packages - Unix & Linux Stack Exchange
I'm on a SUSE machine where the ...2.6/site-packages instead. How do I configure Python so that it also looks in /usr/lib64/python2.6/site-packages? ... I guess that PYTHONPATH could do the job.... More on unix.stackexchange.com
🌐 unix.stackexchange.com
python - add module to pythonpath - nothing works - Stack Overflow
The only variant you didn't try was sys.path.append('/opt/lib/python2.4/site-packages/') which likely would have worked. ... Try $PYTHONPATH instead of $PATH in your bash configuration file. For more info look at the official documentation on this topic. ... Sign up to request clarification or add ... More on stackoverflow.com
🌐 stackoverflow.com
November 10, 2011
python - Permanently add a directory to PYTHONPATH? - Stack Overflow
So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH. More on stackoverflow.com
🌐 stackoverflow.com
Add a folder to the Python library path, once for all (Windows) - Stack Overflow
C:\PythonXY\Lib\site-packages\\ __init__.py module.py subpackage\ __init__.py submodule1.py submodule2.py ... Save this answer. ... Show activity on this post. Set PYTHONPATH environment variable to D:/my_library_folder/ More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › library › site.html
site — Site-specific configuration hook — Python 3.15.2 documentation
3 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 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.

🌐
Note.nkmk.me
note.nkmk.me › home › python
Check and Add the Module Search Path in Python: sys.path | note.nkmk.me
May 12, 2023 - In the above example, the directory '/Users/mbp/Documents/lib' is added to the PYTHONPATH. You can also add module search paths by creating a path configuration file (.pth) in the site-packages directory.
🌐
Foundry
learn.foundry.com › modo › developers › latest › SDK › pages › general › general › Using External Python Modules and the site-packages Directory.html
Using External Python Modules and the site-packages Directory — Modo SDK 14.2v0 documentation
point your script at the system site-packages folder, requiring end users to install, or have installed, Python 2.6 and the required package/module. ship the package/module with your script and use modo’s path alias and import directives to add the package/module to the script’s Python path ...
Find elsewhere
🌐
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.
🌐
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, also sys.path in code, is a list of locations to look for Python packages. Example: >>> import sys >>> sys.path ['', '/usr/local/lib/python2.6', '/usr/local/lib/python2.6/site-packages', ...] >>> import mypackage >>> mypackage.__file__ 'mypackage/__init__.py'
🌐
Real Python
realpython.com › videos › module-search-path
The Module Search Path (Video) – Real Python
I just did an experiment, where I added a directory on my desktop to the PYTHONPATH and was able to import from it. That would be done inside of the .bash_profile on a mac, and probably Linux. It would probably look a bit different on windows. Here are some of the details. stackoverflow.com/questions/3387695/add-to-python-path-mac-os-x · But I would probably stick to the items mentioned in the original article, first most placing the module into the site-packages directory.
Published: January 28, 2020
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
site-packages not importing - Raspberry Pi Forums
May 14, 2022 - export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages · Locked · Print view · 3 posts • Page 1 of 1 · Return to “Python” · Jump to · Community · General discussion · Announcements · Other languages · Deutsch · Español · Français · Italiano ·
🌐
sqlpey
sqlpey.com › python › python-path-methods
Python PATH: Methods to Add Custom Directories for Module Imports
November 4, 2025 - ANS: You can permanently add a directory by modifying your shell’s startup configuration files (like ~/.bashrc or ~/.zshrc on Linux/macOS) to set the PYTHONPATH environment variable, or by creating a .pth file in your Python environment’s site-packages directory.
🌐
Iditect
iditect.com › faq › python › configuring-python-to-use-additional-locations-for-sitepackages.html
Configuring Python to use additional locations for site-packages
This code adds /path/to/custom/site-packages to the Python path, so Python will look for packages in that directory as well. ... You can also set the PYTHONPATH environment variable to include the directories where you want Python to look for packages.
🌐
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.
Top answer
1 of 2
2

(Spyder maintainer here) We forbid adding site-packages directories through our PYTHONPATH manager because it allows people to mix two different Python versions (which is what you're trying to do by adding your system site-packages to your Anaconda's Python).

And we do that because it usually generates odd errors and segfaults for binary packages such as Numpy, Pandas and Matplotlib, given that binary packages for one Python version are incompatible with packages for another one.

Finally, even though you found a workaround for that (by using sys.path), we strongly suggest you to stop doing that because it'll give you nothing by headaches in the future.

2 of 2
-1

Doing what you are asking isn't the recommended path forward but you can solve the underlying problem in either of the following ways (A or B).

To "ensure pip installs packages to another location which Spyder can see" as the asker guessed in a comment on the accepted answer which got no answer (Method B below) is usually not a good idea. Keeping a clean environment for Spyder will ensure that you can determine requirements (including package version) for each of your projects reliably. Therefore, do the reverse of what you guessed: Ensure Spyder uses the Python interpreter in the environment where pip installed your project's required packages.

A. Change the Python interpreter

  1. Go to Tools, Preferences, and set Python interpreter to the python executable that was used to install the package (If using a virtual environment, it would be your_other_env/bin/python).
  2. Close and reopen Spyder (Spyder says to restart the IPython console, but it may not work in this case and show the error where Spyder cannot restart a kernel it didn't start).
  3. Open Spyder again and run any py file. You will get an error that says to install the spyder-kernels package (for some reason pip 22.0.4 will only install spyder_kernels: This issue is at "spyder-kernels should be spyder_kernels" :edit: but the issue is invalid, so upgrade pip first such as via pip install --upgrade pip in your virtual environment). Take note of the version in the error, since that is the version you need.
  4. If you are using conda or are on Windows the instructions will differ, so see Common Illnesses in the Spyder documentation instead of continuing this step.
source your_other_env/bin/activate
pip install --upgrade pip setuptools
pip install spyder-kernels=...
deactivate

but change ... to the version shown in your Spyder error from step 3. If you installed Spyder with conda as recommended, use the commands from the URL above instead.

I don't recommend Method B, as I've explained. However, it may be useful if you are manually installing Spyder plugins or test suites that apply to all projects but aren't in the requirements.txt or setup.py requirements for your project(s) (and therefore don't affect determining requirements for your users).

B. To "ensure pip installs packages to another location which Spyder can see" you would run "spyder_env/bin/python -m pip install ..." to install the package, where spyder_env is the virtualenv where Spyder is installed (but if Spyder is installed in the system using an installer or linux distro package, you may need to use your system's python such as via python3 -m pip install --user ... where ... is the package name. Always use --user instead of sudo or root to avoid mismatched files caused by mashing together the distro-packaged modules and your manually installed modules).

🌐
Reddit
reddit.com › r/learnpython › python3.8 - no site-packages folder in path?
r/learnpython on Reddit: Python3.8 - no site-packages folder in path?
February 21, 2022 -

Hi everyone,

I am having some trouble understanding where to install my Python packages and I would really appreciate some help.

I'm using an external framework (Odoo, but I don't think this is relevant for my question) to develop an app.

I run this framework in the following way:

sudo odoo

The output of which odoo is /usr/bin/odoo and the content of this file is:

#!/usr/bin/python3

# set server timezone in UTC before time module imported
__import__('os').environ['TZ'] = 'UTC'
import odoo

if __name__ == "__main__":
 odoo.cli.main()

I've checked the executable and path with sys.executable and sys.path and I got the following (formatted for easier reading)

executable: /usr/bin/python3
path:
    '/usr/bin',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/usr/local/lib/python3.8/dist-packages',
    '/usr/lib/python3/dist-packages'

However, when I do the same using the python console (i.e.: running /usr/bin/python3) I get:

executable: /usr/bin/python3
path:
    '',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/home/ubuntu/.local/lib/python3.8/site-packages',
    '/usr/local/lib/python3.8/dist-packages',
    '/usr/lib/python3/dist-packages'

Most notably, /home/ubuntu/.local/lib/python3.8/site-packages is present here, and this is the location where pip is installing the packages globally.

This of course means that during execution, the framework can't find any external packages (the site-packages folder isn't in the Python path during the execution of /usr/bin/odoo).

I managed to circumvent this by forcing pip to install my packages in a folder where the framework can see them (in my case, the dist-packages folder), which also required sudo.

I am sure this is a terrible solution, but I couldn't understand what is the best way to do this.

If anyone can share some insights I would really appreciate it!