3.8.5 Diagnostics 3.9 Finding modules Chapter 3. Using Python on Windows 30 • If the Python Home cannot be located, no PYTHONPATH is specified in the environment, and no registry entries can be found, a default path with relative entries is used (e.g. .\Lib;.\plat-win , etc). • If the environment variable PYTHONHOME is set, it is assumed as “Python Home”. Otherwise, the path of the main Python executable is used to locate a “landmark file” (either$_{Lib\os.py}$or$_{pythonXY.zip}$ ) to deduce the “Python Home”. If a Python home is found, the relevant sub-directories added to sys.path ($_{Lib}$ , plat-win , etc) are based on that folder. Otherwise, the core Python path is constructed from the PythonPath stored in the registry. • Additional “application paths” can be added in the registry as subkeys of \SOFTWARE\ Python\PythonCore{version}\PythonPath under both the HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE hives. Subkeys which have semicolon-delimited path strings as their default value will cause each path to be added to$_{sys.path}$ . (Note that all known installers only use HKLM, so HKCU is typically empty.) • If the environment variable PYTHONPATH exists, as described in Environment variables , its entries are added next. Note that on Windows, paths in this variable must be separated by semicolons, to distinguish them from the colon used in drive identifiers ($_{C:\}$etc.). • An empty entry is added at the start, which corresponds to the current directory. When no$_{._pth}$ file is found, this is how$_{sys.path}$is populated on Windows: Note that$_{.pth}$files (without leading underscore) will be processed normally by the$_{site}$ module when$_{import}$ site has been specified. When the file exists, all registry and environment variables are ignored, isolated mode is enabled, and$_{site}$ is not imported unless one line in the file specifies$_{import site}$. Blank paths and lines starting with$_{#}$are ignored. Each path may be absolute or relative to the location of the file. Import statements other than to$_{site}$ are not permitted, and arbitrary code cannot be specified. To completely override$_{sys.path}$ , create a$_{._pth}$ file with the same name as the DLL ($_{python37._pth}$ ) or the executable ($_{python._pth}$ ) and specify one line for each path to add to$_{sys.path}$ . The file based on the DLL name overrides the one based on the executable, which allows paths to be restricted for any program loading the runtime if desired. Python usually stores its library (and thereby your site-packages folder) in the installation directory. So, if you had installed Python to$_{C:\Python\}$, the default library would reside in$_{C:\Python\Lib\}$ and third-party modules should be stored in$_{C:\Python\Lib\site-packages\}$ . If an environment variable PYLAUNCH_DEBUG is set (to any value), the launcher will print diagnostic information to stderr (i.e. to the console). While this information manages to be simultaneously verbose and terse, it should allow you to see what versions of Python were located, why a particular version was chosen and the exact command-line used to execute the target Python. • Setting$_{PY_PYTHON=3}$ and$_{PY_PYTHON3=3.1}$ is equivalent to the INI file containing: • Setting$_{PY_PYTHON=3.1}$ is equivalent to the INI file containing: [defaults] python=3.1 [defaults] python=3 python3=3.1 Python Setup and Usage, Release 3.9.5