Home  >  Article  >  Backend Development  >  How does Python initialize the sys.path list?

How does Python initialize the sys.path list?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 19:05:30261browse

How does Python initialize the sys.path list?

Where is Python's sys.path Initialized From?

Python's sys.path, which contains the directories where Python searches for modules to import, undergoes a complex initialization process to determine its initial values. Here's a breakdown of how it's set up:

sys.executable Determination

  • Python locates its physical location on the filesystem and uses it as the value for sys.executable.

sys.exec_prefix and sys.prefix Initialization

  • Python reads the pyvenv.cfg file in the same directory as sys.executable or one directory up.
  • If the file exists, it extracts the following information:

    • On Windows: If "applocal = true," sys.prefix is set to the directory containing sys.executable. Otherwise, it uses the "home = " directive.
    • On Linux/Mac: Sys.exec_prefix is set to the "home = " directive. Sys.prefix is also set to this value unless the PYTHONHOME environment variable exists.
  • If pyvenv.cfg does not exist or does not contain the necessary information, Python attempts to determine sys.exec_prefix and sys.prefix by walking backward from sys.executable, looking for specific landmark files.

sys.path Initialization

  1. Python adds the directory of the script being executed to sys.path.
  2. On Linux/Mac, it adds the PYTHONPATH environment variable, if set.
  3. It includes the zip file path (/lib/python35.zip or path/to/python.zip).
  4. On Windows and without "applocal = true" in pyvenv.cfg, it adds subkeys from "HK_CURRENT_USERSoftwarePythonPythonCorePythonPath."
  5. On Windows and without "applocal = true" in pyvenv.cfg, and if sys.prefix was not found, it adds the "core contents" of "HK_CURRENT_USERSoftwarePythonPythonCorePythonPath."
  6. On Windows and without "applocal = true" in pyvenv.cfg, it adds subkeys from "HK_LOCAL_MACHINESoftwarePythonPythonCorePythonPath."
  7. On Windows and without "applocal = true" in pyvenv.cfg, and if sys.prefix was not found, it adds the "core contents" of "HK_LOCAL_MACHINESoftwarePythonPythonCorePythonPath."
  8. On Windows and with PYTHONPATH not set, prefix not found, and no registry keys present, it adds the relative compile-time value of PYTHONPATH.
  9. It incorporates paths in the PYTHONPATH compile-time macro relative to the dynamically-found sys.prefix.
  10. On Linux/Mac, it adds sys.exec_prefix. On Windows, it adds the directory used to search for sys.prefix.
  11. On Windows and without "applocal = true" in pyvenv.cfg, it attempts to find sys.prefix by locating landmark files in sys.path. If unsuccessful, it leaves sys.prefix blank.

Additional Modification by the Site Module

  • The site module further modifies sys.path by adding directories based on sys.prefix, sys.exec_prefix, and platform-specific paths.
  • It inspects these new paths for configuration files, potentially adding more paths.

The above is the detailed content of How does Python initialize the sys.path list?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn