|
| 1 | +.. _sys-path-init: |
| 2 | + |
| 3 | +The initialization of the :data:`sys.path` module search path |
| 4 | +============================================================= |
| 5 | + |
| 6 | +A module search path is initialized when Python starts. This module search path |
| 7 | +may be accessed at :data:`sys.path`. |
| 8 | + |
| 9 | +The first entry in the module search path is the directory that contains the |
| 10 | +input script, if there is one. Otherwise, the first entry is the current |
| 11 | +directory, which is the case when executing the interactive shell, a :option:`-c` |
| 12 | +command, or :option:`-m` module. |
| 13 | + |
| 14 | +The :envvar:`PYTHONPATH` environment variable is often used to add directories |
| 15 | +to the search path. If this environment variable is found then the contents are |
| 16 | +added to the module search path. |
| 17 | + |
| 18 | +.. note:: |
| 19 | + |
| 20 | + :envvar:`PYTHONPATH` will affect all installed Python versions/environments. |
| 21 | + Be wary of setting this in your shell profile or global environment variables. |
| 22 | + The :mod:`site` module offers more nuanced techniques as mentioned below. |
| 23 | + |
| 24 | +The next items added are the directories containing standard Python modules as |
| 25 | +well as any :term:`extension module`\s that these modules depend on. Extension |
| 26 | +modules are ``.pyd`` files on Windows and ``.so`` files on other platforms. The |
| 27 | +directory with the platform-independent Python modules is called ``prefix``. |
| 28 | +The directory with the extension modules is called ``exec_prefix``. |
| 29 | + |
| 30 | +The :envvar:`PYTHONHOME` environment variable may be used to set the ``prefix`` |
| 31 | +and ``exec_prefix`` locations. Otherwise these directories are found by using |
| 32 | +the Python executable as a starting point and then looking for various 'landmark' |
| 33 | +files and directories. Note that any symbolic links are followed so the real |
| 34 | +Python executable location is used as the search starting point. The Python |
| 35 | +executable location is called ``home``. |
| 36 | + |
| 37 | +Once ``home`` is determined, the ``prefix`` directory is found by first looking |
| 38 | +for :file:`python{majorversion}{minorversion}.zip` (``python311.zip``). On Windows |
| 39 | +the zip archive is searched for in ``home`` and on Unix the archive is expected |
| 40 | +to be in :file:`lib`. Note that the expected zip archive location is added to the |
| 41 | +module search path even if the archive does not exist. If no archive was found, |
| 42 | +Python on Windows will continue the search for ``prefix`` by looking for :file:`Lib\\os.py`. |
| 43 | +Python on Unix will look for :file:`lib/python{majorversion}.{minorversion}/os.py` |
| 44 | +(``lib/python3.11/os.py``). On Windows ``prefix`` and ``exec_prefix`` are the same, |
| 45 | +however on other platforms :file:`lib/python{majorversion}.{minorversion}/lib-dynload` |
| 46 | +(``lib/python3.11/lib-dynload``) is searched for and used as an anchor for |
| 47 | +``exec_prefix``. On some platforms :file:`lib` may be :file:`lib64` or another value, |
| 48 | +see :data:`sys.platlibdir` and :envvar:`PYTHONPLATLIBDIR`. |
| 49 | + |
| 50 | +Once found, ``prefix`` and ``exec_prefix`` are available at :data:`sys.prefix` and |
| 51 | +:data:`sys.exec_prefix` respectively. |
| 52 | + |
| 53 | +Finally, the :mod:`site` module is processed and :file:`site-packages` directories |
| 54 | +are added to the module search path. A common way to customize the search path is |
| 55 | +to create :mod:`sitecustomize` or :mod:`usercustomize` modules as described in |
| 56 | +the :mod:`site` module documentation. |
| 57 | + |
| 58 | +.. note:: |
| 59 | + |
| 60 | + Certain command line options may further affect path calculations. |
| 61 | + See :option:`-E`, :option:`-I`, :option:`-s` and :option:`-S` for further details. |
| 62 | + |
| 63 | +Virtual environments |
| 64 | +-------------------- |
| 65 | + |
| 66 | +If Python is run in a virtual environment (as described at :ref:`tut-venv`) |
| 67 | +then ``prefix`` and ``exec_prefix`` are specific to the virtual environment. |
| 68 | + |
| 69 | +If a ``pyvenv.cfg`` file is found alongside the main executable, or in the |
| 70 | +directory one level above the executable, the following variations apply: |
| 71 | + |
| 72 | +* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this |
| 73 | + path is used instead of the path to the main executable when deducing ``prefix`` |
| 74 | + and ``exec_prefix``. |
| 75 | + |
| 76 | +_pth files |
| 77 | +---------- |
| 78 | + |
| 79 | +To completely override :data:`sys.path` create a ``._pth`` file with the same |
| 80 | +name as the shared library or executable (``python._pth`` or ``python311._pth``). |
| 81 | +The shared library path is always known on Windows, however it may not be |
| 82 | +available on other platforms. In the ``._pth`` file specify one line for each path |
| 83 | +to add to :data:`sys.path`. The file based on the shared library name overrides |
| 84 | +the one based on the executable, which allows paths to be restricted for any |
| 85 | +program loading the runtime if desired. |
| 86 | + |
| 87 | +When the file exists, all registry and environment variables are ignored, |
| 88 | +isolated mode is enabled, and :mod:`site` is not imported unless one line in the |
| 89 | +file specifies ``import site``. Blank paths and lines starting with ``#`` are |
| 90 | +ignored. Each path may be absolute or relative to the location of the file. |
| 91 | +Import statements other than to ``site`` are not permitted, and arbitrary code |
| 92 | +cannot be specified. |
| 93 | + |
| 94 | +Note that ``.pth`` files (without leading underscore) will be processed normally |
| 95 | +by the :mod:`site` module when ``import site`` has been specified. |
| 96 | + |
| 97 | +Embedded Python |
| 98 | +--------------- |
| 99 | + |
| 100 | +If Python is embedded within another application :c:func:`Py_InitializeFromConfig` and |
| 101 | +the :c:type:`PyConfig` structure can be used to initialize Python. The path specific |
| 102 | +details are described at :ref:`init-path-config`. Alternatively the older :c:func:`Py_SetPath` |
| 103 | +can be used to bypass the initialization of the module search path. |
| 104 | + |
| 105 | +.. seealso:: |
| 106 | + |
| 107 | + * :ref:`windows_finding_modules` for detailed Windows notes. |
| 108 | + * :ref:`using-on-unix` for Unix details. |
0 commit comments