Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it reliable and documented how PYTHONPATH populates the sys.path?

On my machine, the values from PYTHONPATH appear to get inserted in sys.path:

  • beginning at index 1
  • order preserved
  • de-duplicated

For example, with PYTHONPATH=/spam:/eggs:/spam and then checking in python -m site, I get a result like:

sys.path = [
    something,
    '/spam',
    '/eggs',
    more,
    stuff,
    after
]

It seems to be the same behaviour on Python 2 and Python 3. The question is, how much of this handling of PYTHONPATH is documented / reliable, and what if any might be different on other platforms? Is this baked into the interpreter, or is handled by site.py and/or in danger of being "tweaked" by sysadmins?

I can't see it explained in the documentation here, it just says sys.path is "augmented" (and, contrary to the documentation, non-existent directories do not appear to be silently ignored).

like image 252
wim Avatar asked Feb 28 '18 01:02

wim


People also ask

Is Pythonpath same as SYS path?

PYTHONPATH is related to sys. path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import .

How does Python determine SYS path?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. 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.

Does Python use path or Pythonpath?

Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules.

What is the purpose of Pythonpath environment variable?

So, the only reason to use PYTHONPATH variables is to maintain directories of custom Python libraries that are not installed in the site packages directory (the global default location). In simple terms, it is used by user-defined modules to set the path so that they can be directly imported into a Python program.


1 Answers

Let's go down the list.


  • beginning at index 1

That's reliable. As stated in the PYTHONPATH docs,

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.

One directory is inserted before PYTHONPATH, which may be the current directory, the script directory, or some other directory depending on how you ran Python. Other directories are appended. The site module will also add some modules to sys.path, but site appends too:

Importing this module will append site-specific paths to the module search path and add a few builtins...


  • order preserved

I don't think this is explicitly documented anywhere, but search path order is important, and changing it is a backward compatibility break I don't think they would make lightly.


  • de-duplicated

That's an undocumented effect of the site module. It won't happen if you run Python with the -S flag that disables site. You can see the code in site.removeduppaths

like image 133
user2357112 supports Monica Avatar answered Sep 21 '22 13:09

user2357112 supports Monica