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 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 ...
🌐
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.
Find elsewhere
🌐
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'
🌐
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 ·
🌐
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
🌐
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.
🌐
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 - So in order to write some code locally and ensure that when we start python somewhere we can import the code we need to: ... somewhere in one of the directories in sys.path (usually site-packages) add a file called module_name.pth that contains ...
🌐
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).

🌐
Python Tutorial
pythontutorial.net › home › python basics › python module search path
Python Module Search Path
October 15, 2020 - C:pythonpython313.zip C:pythonDLLs C:pythonLib C:python C:pythonLibsite-packagesCopy · To make sure Python can always find the module.py, you need to: Place module.py in the folder where the program will execute. Include the folder that contains the module.py in the PYTHONPATH environment variable.