Everything shown in this
sys.path is “ordinary” and would not require you to take any action yourself to set the search path.
sys.path will have several paths added automatically when Python starts. Directories named
site-packages get added by a standard library module called
site, which runs at startup by default. You can use the
-s flag for Python to tell it not to add a
site-packages directory, and
-S to tell it not to run
site.py at all.
In your case,
/home/debian/.local/lib/python3.9/site-packages is the default
site-packages folder for user-level installs of third-party packages (such as Numpy or Pandas) using Pip.
It looks like you are using an installation of Python that came with your OS; it will probably be protected against installing third-party packages as an admin. Please do not try to work around this; if anyone manages to get malware onto PyPI, this would greatly increase the damage it can do, and even a normal package could (in theory) accidentally break an important system utility script. If you need to add something directly to the system Python, use Debian’s package manager to do it. But please first consider using a user-level installation (
pip install --user) or installing in a virtual environment instead.
A complete breakdown:
The empty string is added at the front of
sys.path when you start the interpreter from the command line normally (just
python instead of running a script). This allows
import statements in the interpreter to find Python code that is in the current working directory. It’s a relative path (the same as
'.') so if you e.g. use
os.chdir, it will change what you can import normally.
In 3.11 and up, you can specifically prevent this by using the
-P option for Python, or setting the
PYTHONSAFEPATH environment variable. In older versions (since 3.4), you can use
-I (“isolated mode”), but this will also disable adding the site-package directory (like
-s) and ignore all the special env variables like
'/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload'
These are paths for the standard library. There might not be a
python39.zip on your system but it will be added anyway. Python can look in zip files as well as folders to find Python code, which is pretty cool
This is where packages go when you do something like
pip install --user package-installation-test. This path will be added to
sys.path even if you don’t have Pip in the system Python (some Linux distros don’t include it by default - mine doesn’t). There will be a separate such folder for each user, of course.
'/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages'
These are folders for packages that you install using the system package manager.
For even more information, you can also read the documentation for the
site module, as well as in the actual
site.py source code. This module is also what makes
exit work in the interpreter.