17db96d56Sopenharmony_ci.. _sys-path-init: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ciThe initialization of the :data:`sys.path` module search path 47db96d56Sopenharmony_ci============================================================= 57db96d56Sopenharmony_ci 67db96d56Sopenharmony_ciA module search path is initialized when Python starts. This module search path 77db96d56Sopenharmony_cimay be accessed at :data:`sys.path`. 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ciThe first entry in the module search path is the directory that contains the 107db96d56Sopenharmony_ciinput script, if there is one. Otherwise, the first entry is the current 117db96d56Sopenharmony_cidirectory, which is the case when executing the interactive shell, a :option:`-c` 127db96d56Sopenharmony_cicommand, or :option:`-m` module. 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThe :envvar:`PYTHONPATH` environment variable is often used to add directories 157db96d56Sopenharmony_cito the search path. If this environment variable is found then the contents are 167db96d56Sopenharmony_ciadded to the module search path. 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ci.. note:: 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ci :envvar:`PYTHONPATH` will affect all installed Python versions/environments. 217db96d56Sopenharmony_ci Be wary of setting this in your shell profile or global environment variables. 227db96d56Sopenharmony_ci The :mod:`site` module offers more nuanced techniques as mentioned below. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ciThe next items added are the directories containing standard Python modules as 257db96d56Sopenharmony_ciwell as any :term:`extension module`\s that these modules depend on. Extension 267db96d56Sopenharmony_cimodules are ``.pyd`` files on Windows and ``.so`` files on other platforms. The 277db96d56Sopenharmony_cidirectory with the platform-independent Python modules is called ``prefix``. 287db96d56Sopenharmony_ciThe directory with the extension modules is called ``exec_prefix``. 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ciThe :envvar:`PYTHONHOME` environment variable may be used to set the ``prefix`` 317db96d56Sopenharmony_ciand ``exec_prefix`` locations. Otherwise these directories are found by using 327db96d56Sopenharmony_cithe Python executable as a starting point and then looking for various 'landmark' 337db96d56Sopenharmony_cifiles and directories. Note that any symbolic links are followed so the real 347db96d56Sopenharmony_ciPython executable location is used as the search starting point. The Python 357db96d56Sopenharmony_ciexecutable location is called ``home``. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ciOnce ``home`` is determined, the ``prefix`` directory is found by first looking 387db96d56Sopenharmony_cifor :file:`python{majorversion}{minorversion}.zip` (``python311.zip``). On Windows 397db96d56Sopenharmony_cithe zip archive is searched for in ``home`` and on Unix the archive is expected 407db96d56Sopenharmony_cito be in :file:`lib`. Note that the expected zip archive location is added to the 417db96d56Sopenharmony_cimodule search path even if the archive does not exist. If no archive was found, 427db96d56Sopenharmony_ciPython on Windows will continue the search for ``prefix`` by looking for :file:`Lib\\os.py`. 437db96d56Sopenharmony_ciPython on Unix will look for :file:`lib/python{majorversion}.{minorversion}/os.py` 447db96d56Sopenharmony_ci(``lib/python3.11/os.py``). On Windows ``prefix`` and ``exec_prefix`` are the same, 457db96d56Sopenharmony_cihowever on other platforms :file:`lib/python{majorversion}.{minorversion}/lib-dynload` 467db96d56Sopenharmony_ci(``lib/python3.11/lib-dynload``) is searched for and used as an anchor for 477db96d56Sopenharmony_ci``exec_prefix``. On some platforms :file:`lib` may be :file:`lib64` or another value, 487db96d56Sopenharmony_cisee :data:`sys.platlibdir` and :envvar:`PYTHONPLATLIBDIR`. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ciOnce found, ``prefix`` and ``exec_prefix`` are available at :data:`sys.prefix` and 517db96d56Sopenharmony_ci:data:`sys.exec_prefix` respectively. 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ciFinally, the :mod:`site` module is processed and :file:`site-packages` directories 547db96d56Sopenharmony_ciare added to the module search path. A common way to customize the search path is 557db96d56Sopenharmony_cito create :mod:`sitecustomize` or :mod:`usercustomize` modules as described in 567db96d56Sopenharmony_cithe :mod:`site` module documentation. 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ci.. note:: 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci Certain command line options may further affect path calculations. 617db96d56Sopenharmony_ci See :option:`-E`, :option:`-I`, :option:`-s` and :option:`-S` for further details. 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ciVirtual environments 647db96d56Sopenharmony_ci-------------------- 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ciIf Python is run in a virtual environment (as described at :ref:`tut-venv`) 677db96d56Sopenharmony_cithen ``prefix`` and ``exec_prefix`` are specific to the virtual environment. 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ciIf a ``pyvenv.cfg`` file is found alongside the main executable, or in the 707db96d56Sopenharmony_cidirectory one level above the executable, the following variations apply: 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci* If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this 737db96d56Sopenharmony_ci path is used instead of the path to the main executable when deducing ``prefix`` 747db96d56Sopenharmony_ci and ``exec_prefix``. 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci_pth files 777db96d56Sopenharmony_ci---------- 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ciTo completely override :data:`sys.path` create a ``._pth`` file with the same 807db96d56Sopenharmony_ciname as the shared library or executable (``python._pth`` or ``python311._pth``). 817db96d56Sopenharmony_ciThe shared library path is always known on Windows, however it may not be 827db96d56Sopenharmony_ciavailable on other platforms. In the ``._pth`` file specify one line for each path 837db96d56Sopenharmony_cito add to :data:`sys.path`. The file based on the shared library name overrides 847db96d56Sopenharmony_cithe one based on the executable, which allows paths to be restricted for any 857db96d56Sopenharmony_ciprogram loading the runtime if desired. 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ciWhen the file exists, all registry and environment variables are ignored, 887db96d56Sopenharmony_ciisolated mode is enabled, and :mod:`site` is not imported unless one line in the 897db96d56Sopenharmony_cifile specifies ``import site``. Blank paths and lines starting with ``#`` are 907db96d56Sopenharmony_ciignored. Each path may be absolute or relative to the location of the file. 917db96d56Sopenharmony_ciImport statements other than to ``site`` are not permitted, and arbitrary code 927db96d56Sopenharmony_cicannot be specified. 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ciNote that ``.pth`` files (without leading underscore) will be processed normally 957db96d56Sopenharmony_ciby the :mod:`site` module when ``import site`` has been specified. 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ciEmbedded Python 987db96d56Sopenharmony_ci--------------- 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ciIf Python is embedded within another application :c:func:`Py_InitializeFromConfig` and 1017db96d56Sopenharmony_cithe :c:type:`PyConfig` structure can be used to initialize Python. The path specific 1027db96d56Sopenharmony_cidetails are described at :ref:`init-path-config`. Alternatively the older :c:func:`Py_SetPath` 1037db96d56Sopenharmony_cican be used to bypass the initialization of the module search path. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci.. seealso:: 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci * :ref:`windows_finding_modules` for detailed Windows notes. 1087db96d56Sopenharmony_ci * :ref:`using-on-unix` for Unix details. 109