
The directory of the script which python is executing is added to sys.path.Then, (what you've all been waiting for,) python determines the initial values Until sys.path is fully figured out to set a fallback value for Values of sys.prefix and sys.exec_prefix. Linux and Mac, for example, use pre-compiled defaults as the If it can't find these "landmark" files or sys.prefix hasn't beenįound yet, then python sets sys.prefix to a "fallback" Used or, if home is set in pyvenv.cfg, that is used instead for Either the directory of sys.executable is
#PYTHON BLACK FORMATTER SYS.PATH WINDOWS#
This entire step is skipped on Windows ifĪpplocal = true is set. Mac, and Windows, with sys.exec_prefix set to the same value as Subdirectories, that directory is set to be sys.prefix on Linux, Lib/python/os.py is is found in the directory or any of its Or any of its parent directories, that directory is set to be to be If the file lib/python/dyn-load is found in that directory Otherwise, these sys.prefix and sys.exec_prefix are found by walking backwardsįrom the location of sys.executable, or the home directory given by pyvenv.cfg if any. If it exists, unless a home = setting is present in pyvenv.cfg, Sys.prefix and sys.exec_prefix is set to the PYTHONHOME environment variable, It exists, superseding any home = setting in pyvenv.cfg. Sys.prefix and sys.exec_prefix are set to the PYTHONHOME environment variable, if Next, the PYTHONHOME environment variable is examined.

Then sys.prefix will be set to the directory containing sys.executable. Pyvenv.cfg file on Windows, but not the home = setting, If the applocal = true setting appears in the

When it dynamically sets the initial value of sys.prefix later. Python will use this directory instead of the directory containing sys.executable One of the values in this config file that python looks for is Sys.executable or one directory up, python looks at it. If there is a file called pyvenv.cfg in the same directory as Next, it determines the initial values for sys.exec_prefix and

The executable that it finds is used as the value for sys.executable, no ifs, If the OS just says "python" is running, it finds itself in $PATH.

Location on the filesystem based on what the operating system tells Sys.executable, sys.exec_prefix, and sys.prefix on a normalįirst, python does its level best to figure out its actual physical Somewhat-incomplete, somewhat-wrong, but hopefully-useful guideįor the rank-and-file python programmer of what happens when pythonįigures out what to use as the initial values of sys.path, Python really tries hard to intelligently set sys.path. I still believe my answer to be relevant and relatively current.
#PYTHON BLACK FORMATTER SYS.PATH CODE#
There is also a prose explanation of the algorithm in the comments of getpath.py in the code base. There is now per the comments, if you wanted to also check that out. When I wrote this back in 2015, there was no documentation on the subject. I wonder where those paths before PYTHONPATH's ones come from? UPD: Python is adding some paths before refering to PYTHONPATH: > import sys Where is Python's sys.path initialized from?
