[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How python knows where non standard libraries are stored ?

On 9/7/19, ast <none at> wrote:
> Eg on my system, here is the content of sys.path:
>  >>> import sys
>  >>> sys.path
> ['',

In the REPL, "" is added for loading modules from the current
directory. When executing a script, this would be the script

> 'C:\\Users\\jean-marc\\Desktop\\python',

Probably this directory is in your %PYTHONPATH% environment variable,
which gets inserted here, normally ahead of everything else except for
the script directory.

> 'C:\\Program Files\\Python36-32\\',

The zipped standard-library location is assumed to be beside the DLL or EXE.

Next the interpreter adds the PythonPath directories from the
registry. These are found in subkeys of
r"[HKLM|HKCU]\Python\PythonCore\3.6-32\PythonPath". The base key has
default core paths for the standard library, which normally are
ignored unless the interpreter can't find its home directory.

> 'C:\\Program Files\\Python36-32\\DLLs',
> 'C:\\Program Files\\Python36-32\\lib',

These two are derived from the default core standard-library paths,
which are hard-coded in the C macro, PYTHONPATH:

    #define PYTHONPATH L".\\DLLs;.\\lib"

At startup the interpreter searches for its home directory if
PYTHONHOME isn't set. (Normally it should not be set.) If the zipped
standard library exists, its directory is used as the home directory.
Otherwise it checks for the landmark module "lib/" in the
application directory (i.e. argv0_path), and its ancestor directories
down to the drive root. (If we're executing a virtual environment, the
argv0_path gets set from the "home" value in its pyvenv.cfg file.)
Normally the home directory is argv0_path.

The home directory is used to resolve the "." components in the
hard-coded PYTHONPATH string. If no home directory has been found, the
interpreter uses the default core paths from the "PythonPath" registry
key as discussed above. If even that isn't found, it just adds the
relative paths, ".\\DLLs" and ".\\lib".

> 'C:\\Program Files\\Python36-32',

Windows Python has this peculiar addition. It always adds argv0_path
(typically the application directory). Perhaps at some time in the
past it was necessary because extension modules were located here.
AFAIK, this is vestigial now, unless some embedding applications rely
on it.

At this point if it still hasn't found the home directory, the
interpreter checks for the "lib/" landmark in all of the
directories that have been added to the module search path. This is a
last-ditch effort to find the standard library and set sys.prefix.

> 'C:\\Program Files\\Python36-32\\lib\\site-packages']

Now we're into the site module additions, including .pth files, which
is pretty well documented via help(site) and the docs:

The -S command-line option prevents importing the site module at startup.