The site module documentation and Modifying Python's Search Path seem to be what you're looking for.

As far as I understand it, those entries are being added to sys.path by:

  • /usr/lib/python2.6/site.py
  • /usr/lib/python2.6/dist-packages/site.py
    (Change 2.6 to your version of Python.)

The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.

Alternatively, maybe you can teach the package to use site.getsitepackages()?

Answer from Mikel on Stack Exchange
Discussions

Sys.path is included in the current directory by default
Hi, I am trying a custom build of python 3.11 on linux, I see that sys.path has current directory by default, I would like to change this behavior by changing the cpython source, >>> import sys >>> sys.path ['', '/usr/local/lib/python311.zip', '/home/mohammed/python/Python-3.11.0/Lib', ... More on discuss.python.org
🌐 discuss.python.org
6
0
March 29, 2023
How to temporarily modify sys.path in Python? - Stack Overflow
Appending a value to sys.path only modifies it temporarily, i.e for that session only. Permanent modifications are done by changing PYTHONPATH and the default installation directory. More on stackoverflow.com
🌐 stackoverflow.com
Change the priority of python sys.path - Stack Overflow
How can I change the priority of the path in sys.path in python 2.7? I know that I can use PYTHONPATH environment variable, but it is what I will get: $ PYTHONPATH=/tmp python Python 2.7.6 (defaul... More on stackoverflow.com
🌐 stackoverflow.com
packages - Best way to set sys.path for "hot library" development in Python? - Software Engineering Stack Exchange
I have multiple copies of this ... set PYTHONPATH in my shell without some pain. (I would need to remember to change it each time I switched to work on a different branch, and I am very forgetful) Instead, I have some logic that walks up the file tree, starting at the "current" file location, moving from leaf towards root, looking for branch_root_marker. Once the root directory of the current working copy is found, it adds library/ and productline/ to sys.path... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 7, 2012
🌐
Xmodulo
xmodulo.com › change-syspath-pythonpath-python.html
How to change sys.path or PYTHONPATH in Python
September 23, 2020 - By default, sys.path is constructed as a concatenation of (1) the current working directory, (2) content of PYTHONPATH environment variable, and (3) a set of default paths supplied by the installed Python interpreter.
🌐
YouTube
youtube.com › watch
Python Programming 53 - Sys.path and Changing Module Paths - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals 💯 FREE Courses (100+ hours) - https://calcur.tech/all-in-ones🐍 Python Course - https:...
Published: September 10, 2020
🌐
Python
docs.python.org › 3 › library › sys_path_init.html
The initialization of the sys.path module search path — Python 3.15.2 documentation
To completely override sys.path create a ._pth file with the same name as the shared library or executable (python._pth or python311._pth). The shared library path is always known on Windows, however it may not be available on other platforms.
🌐
Python.org
discuss.python.org › python help
Sys.path is included in the current directory by default - Python Help - Discussions on Python.org
March 29, 2023 - Hi, I am trying a custom build of python 3.11 on linux, I see that sys.path has current directory by default, I would like to change this behavior by changing the cpython source, >>> import sys >>> sys.path ['', '/usr/local/lib/python311.zip', '/home/mohammed/python/Python-3.11.0/Lib', ...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Modify sys.path in Python3 - Raspberry Pi Forums
When Python starts, the CWD is added at the top of sys.path. sys.path contains the search paths for import statements. I think you are talking about Idle and its open file command (menu). The default path also depends on the CWD.
Find elsewhere
Top answer
1 of 2
9

As you may know, sys.path is initialized from:

  • the current directory
  • your PYTHONPATH
  • an installation-dependent default

However unfortunately that is only part of the story: setuptools creates easy-install.pth files, which also modify sys.path and worst of all they prepend packages and therefore totally mess up the order of directories.

In particular (at least on my system), there is /usr/local/lib/python2.7/dist-packages/easy-install.pth with the following contents:

import sys; sys.__plen = len(sys.path)
/usr/lib/python2.7/dist-packages
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

This causes /usr/lib/python2.7/dist-packages to be prepended even before your PYTHONPATH!

What you could do is simply change the 2nd line in this file to

/usr/local/lib/python2.7/dist-packages

and you will get your desired priority.

However beware this file might be overwritten or changed again by a future setuptools invocation!

2 of 2
2

We encountered an almost identical situation and wanted to expand upon @kynan's response which is spot-on. In the case where you have such an easy-install.pth that you want to overcome, but which you cannot modify it (say you are a user with no root/admin access), you can do the following:

  • Set up an alternate python installation scheme

    • e.g. we use a PYTHON HOME install (setting PYTHONUSERBASE)
  • Create a user/home site-packages

    • You can do this by installing a package into the user env: pip install <package> --user
  • Create a pth to set sys.__egginsert to workaround the system/distribution easy-install.pth

    • Create a $PYTHONUSERBASE/lib/python2.7/site-packages/fix_easy_install.pth
    • Containing: import sys; sys.__egginsert = len(sys.path);

This will set sys.__egginsert to point to the end of your sys.path including your usersite paths. When the nefarious system/dist easy-install.pth will then insert its items at the end of the system path.

🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch04s23.html
Dynamically Changing the Python Search Path - Python Cookbook [Book]
July 19, 2002 - Windows is case-insensitive, so lowercase # for definiteness. new_path = os.path.abspath(new_path) if sys.platform == 'win32': new_path = new_path.lower( ) # Check against all currently available paths for x in sys.path: x = os.path.abspath(x) if sys.platform == 'win32': x = x.lower( ) if new_path in (x, x + os.sep): return 0 sys.path.append(new_path) return 1 if _ _name_ _ == '_ _main_ _': # Test and show usage import sys print 'Before:' for x in sys.path: print x if sys.platform == 'win32': print AddSysPath('c:\\Temp') print AddSysPath('c:\\temp') else: print AddSysPath('usr/lib/my_modules') print 'After:' for x in sys.path: print x · Modules must be on the Python search path before they can be imported, but we don’t want to have a huge permanent path, ...
Authors: Alex MartelliDavid Ascher
Published: 2002
Pages: 608
🌐
Reddit
reddit.com › r/learnpython › is it safe or unsafe to add to sys.path?
r/learnpython on Reddit: Is it safe or unsafe to add to sys.path?
October 31, 2017 -

Recently, I came to know that some Python guru's prefer not to touch sys.path. I wanted to know if it is safe or unsafe to modify sys.path by appending paths to get them in PYTHONPATH? If it is unsafe then which is better way to get a path in PYTHONPATH?

🌐
The Coding Forums
thecodingforums.com › archive › archive › python
changing sys.path | Python | Coding Forums
February 10, 2012 - So suppose I want to modify the sys.path on the fly before running some code which imports from one of the modules added. at run time I do sys.path.extend(paths_to_add) but it still doesn't work and I get an import error. If I take these paths and add them to site-packages/my_paths.pth...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sys-path-in-python
sys.path in Python - GeeksforGeeks
August 21, 2024 - When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules. If not found it looks through the list of directories(a directory is a folder that contains related modules) defined by sys.path. ... DEFAULT- By default, the interpreter looks for a module within the current directory. To make the interpreter search in some other directory you just simply have to change the current directory.
🌐
YouTube
youtube.com › watch
PYTHON sys.path and PYTHONPATH - YouTube
Where does Python look to find modules and packages? How can you adjust the Python search path so that modules that you create can be located.
Published: July 23, 2021
🌐
Stack Overflow
stackoverflow.com › questions › 64975585 › how-do-i-modify-the-sys-path-in-python
How do I modify the sys.path in Python? - Stack Overflow
It wouldn't do that on its own, which makes me think you used PYTHONPATH incorrectly (read: at all; it's a hack that's bites you constantly when you need to support multiple versions of Python). echo $PYTHONPATH in your regular shell (not Python); I'll guess you made it something terrible (possibly in your .bashrc). You could remove the bad entries from sys.path (it's a plain list, with list methods), but it's best not to have those entries.
🌐
Linux Hint
linuxhint.com › sys-path-append-python
How to Use Sys.path.append() in Python – Linux Hint
In all cases, we cannot keep every single Python file we create in one directory, we may wish to use multiple directories to adequately classify our files. The problem however is that we may need to import our created modules left and right. In such cases, if we want to prevent a ModuleNotFound Error, then we can add the location of the directory – and mind you, it has to be a directory, not a file – to sys.path using the sys.path.append() method.