17db96d56Sopenharmony_ci:mod:`runpy` --- Locating and executing Python modules
27db96d56Sopenharmony_ci======================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: runpy
57db96d56Sopenharmony_ci   :synopsis: Locate and run Python modules without importing them first.
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci.. moduleauthor:: Nick Coghlan <ncoghlan@gmail.com>
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci**Source code:** :source:`Lib/runpy.py`
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci--------------
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ciThe :mod:`runpy` module is used to locate and run Python modules without
147db96d56Sopenharmony_ciimporting them first. Its main use is to implement the :option:`-m` command
157db96d56Sopenharmony_ciline switch that allows scripts to be located using the Python module
167db96d56Sopenharmony_cinamespace rather than the filesystem.
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ciNote that this is *not* a sandbox module - all code is executed in the
197db96d56Sopenharmony_cicurrent process, and any side effects (such as cached imports of other
207db96d56Sopenharmony_cimodules) will remain in place after the functions have returned.
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ciFurthermore, any functions and classes defined by the executed code are not
237db96d56Sopenharmony_ciguaranteed to work correctly after a :mod:`runpy` function has returned.
247db96d56Sopenharmony_ciIf that limitation is not acceptable for a given use case, :mod:`importlib`
257db96d56Sopenharmony_ciis likely to be a more suitable choice than this module.
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ciThe :mod:`runpy` module provides two functions:
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci.. function:: run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci   .. index::
337db96d56Sopenharmony_ci      pair: module; __main__
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci   Execute the code of the specified module and return the resulting module
367db96d56Sopenharmony_ci   globals dictionary. The module's code is first located using the standard
377db96d56Sopenharmony_ci   import mechanism (refer to :pep:`302` for details) and then executed in a
387db96d56Sopenharmony_ci   fresh module namespace.
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ci   The *mod_name* argument should be an absolute module name.
417db96d56Sopenharmony_ci   If the module name refers to a package rather than a normal
427db96d56Sopenharmony_ci   module, then that package is imported and the ``__main__`` submodule within
437db96d56Sopenharmony_ci   that package is then executed and the resulting module globals dictionary
447db96d56Sopenharmony_ci   returned.
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci   The optional dictionary argument *init_globals* may be used to pre-populate
477db96d56Sopenharmony_ci   the module's globals dictionary before the code is executed. The supplied
487db96d56Sopenharmony_ci   dictionary will not be modified. If any of the special global variables
497db96d56Sopenharmony_ci   below are defined in the supplied dictionary, those definitions are
507db96d56Sopenharmony_ci   overridden by :func:`run_module`.
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci   The special global variables ``__name__``, ``__spec__``, ``__file__``,
537db96d56Sopenharmony_ci   ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
547db96d56Sopenharmony_ci   dictionary before the module code is executed (Note that this is a
557db96d56Sopenharmony_ci   minimal set of variables - other variables may be set implicitly as an
567db96d56Sopenharmony_ci   interpreter implementation detail).
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci   ``__name__`` is set to *run_name* if this optional argument is not
597db96d56Sopenharmony_ci   :const:`None`, to ``mod_name + '.__main__'`` if the named module is a
607db96d56Sopenharmony_ci   package and to the *mod_name* argument otherwise.
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci   ``__spec__`` will be set appropriately for the *actually* imported
637db96d56Sopenharmony_ci   module (that is, ``__spec__.name`` will always be *mod_name* or
647db96d56Sopenharmony_ci   ``mod_name + '.__main__``, never *run_name*).
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci   ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` are
677db96d56Sopenharmony_ci   :ref:`set as normal <import-mod-attrs>` based on the module spec.
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci   If the argument *alter_sys* is supplied and evaluates to :const:`True`,
707db96d56Sopenharmony_ci   then ``sys.argv[0]`` is updated with the value of ``__file__`` and
717db96d56Sopenharmony_ci   ``sys.modules[__name__]`` is updated with a temporary module object for the
727db96d56Sopenharmony_ci   module being executed. Both ``sys.argv[0]`` and ``sys.modules[__name__]``
737db96d56Sopenharmony_ci   are restored to their original values before the function returns.
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci   Note that this manipulation of :mod:`sys` is not thread-safe. Other threads
767db96d56Sopenharmony_ci   may see the partially initialised module, as well as the altered list of
777db96d56Sopenharmony_ci   arguments. It is recommended that the :mod:`sys` module be left alone when
787db96d56Sopenharmony_ci   invoking this function from threaded code.
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci   .. seealso::
817db96d56Sopenharmony_ci      The :option:`-m` option offering equivalent functionality from the
827db96d56Sopenharmony_ci      command line.
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   .. versionchanged:: 3.1
857db96d56Sopenharmony_ci      Added ability to execute packages by looking for a ``__main__`` submodule.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci   .. versionchanged:: 3.2
887db96d56Sopenharmony_ci      Added ``__cached__`` global variable (see :pep:`3147`).
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci   .. versionchanged:: 3.4
917db96d56Sopenharmony_ci      Updated to take advantage of the module spec feature added by
927db96d56Sopenharmony_ci      :pep:`451`. This allows ``__cached__`` to be set correctly for modules
937db96d56Sopenharmony_ci      run this way, as well as ensuring the real module name is always
947db96d56Sopenharmony_ci      accessible as ``__spec__.name``.
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci.. function:: run_path(path_name, init_globals=None, run_name=None)
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci   .. index::
997db96d56Sopenharmony_ci      pair: module; __main__
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci   Execute the code at the named filesystem location and return the resulting
1027db96d56Sopenharmony_ci   module globals dictionary. As with a script name supplied to the CPython
1037db96d56Sopenharmony_ci   command line, the supplied path may refer to a Python source file, a
1047db96d56Sopenharmony_ci   compiled bytecode file or a valid sys.path entry containing a ``__main__``
1057db96d56Sopenharmony_ci   module (e.g. a zipfile containing a top-level ``__main__.py`` file).
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci   For a simple script, the specified code is simply executed in a fresh
1087db96d56Sopenharmony_ci   module namespace. For a valid sys.path entry (typically a zipfile or
1097db96d56Sopenharmony_ci   directory), the entry is first added to the beginning of ``sys.path``. The
1107db96d56Sopenharmony_ci   function then looks for and executes a :mod:`__main__` module using the
1117db96d56Sopenharmony_ci   updated path. Note that there is no special protection against invoking
1127db96d56Sopenharmony_ci   an existing :mod:`__main__` entry located elsewhere on ``sys.path`` if
1137db96d56Sopenharmony_ci   there is no such module at the specified location.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci   The optional dictionary argument *init_globals* may be used to pre-populate
1167db96d56Sopenharmony_ci   the module's globals dictionary before the code is executed. The supplied
1177db96d56Sopenharmony_ci   dictionary will not be modified. If any of the special global variables
1187db96d56Sopenharmony_ci   below are defined in the supplied dictionary, those definitions are
1197db96d56Sopenharmony_ci   overridden by :func:`run_path`.
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci   The special global variables ``__name__``, ``__spec__``, ``__file__``,
1227db96d56Sopenharmony_ci   ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
1237db96d56Sopenharmony_ci   dictionary before the module code is executed (Note that this is a
1247db96d56Sopenharmony_ci   minimal set of variables - other variables may be set implicitly as an
1257db96d56Sopenharmony_ci   interpreter implementation detail).
1267db96d56Sopenharmony_ci
1277db96d56Sopenharmony_ci   ``__name__`` is set to *run_name* if this optional argument is not
1287db96d56Sopenharmony_ci   :const:`None` and to ``'<run_path>'`` otherwise.
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci   If the supplied path directly references a script file (whether as source
1317db96d56Sopenharmony_ci   or as precompiled byte code), then ``__file__`` will be set to the
1327db96d56Sopenharmony_ci   supplied path, and ``__spec__``, ``__cached__``, ``__loader__`` and
1337db96d56Sopenharmony_ci   ``__package__`` will all be set to :const:`None`.
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci   If the supplied path is a reference to a valid sys.path entry, then
1367db96d56Sopenharmony_ci   ``__spec__`` will be set appropriately for the imported ``__main__``
1377db96d56Sopenharmony_ci   module (that is, ``__spec__.name`` will always be ``__main__``).
1387db96d56Sopenharmony_ci   ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
1397db96d56Sopenharmony_ci   :ref:`set as normal <import-mod-attrs>` based on the module spec.
1407db96d56Sopenharmony_ci
1417db96d56Sopenharmony_ci   A number of alterations are also made to the :mod:`sys` module. Firstly,
1427db96d56Sopenharmony_ci   ``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated
1437db96d56Sopenharmony_ci   with the value of ``path_name`` and ``sys.modules[__name__]`` is updated
1447db96d56Sopenharmony_ci   with a temporary module object for the module being executed. All
1457db96d56Sopenharmony_ci   modifications to items in :mod:`sys` are reverted before the function
1467db96d56Sopenharmony_ci   returns.
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   Note that, unlike :func:`run_module`, the alterations made to :mod:`sys`
1497db96d56Sopenharmony_ci   are not optional in this function as these adjustments are essential to
1507db96d56Sopenharmony_ci   allowing the execution of sys.path entries. As the thread-safety
1517db96d56Sopenharmony_ci   limitations still apply, use of this function in threaded code should be
1527db96d56Sopenharmony_ci   either serialised with the import lock or delegated to a separate process.
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci   .. seealso::
1557db96d56Sopenharmony_ci      :ref:`using-on-interface-options` for equivalent functionality on the
1567db96d56Sopenharmony_ci      command line (``python path/to/script``).
1577db96d56Sopenharmony_ci
1587db96d56Sopenharmony_ci   .. versionadded:: 3.2
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci   .. versionchanged:: 3.4
1617db96d56Sopenharmony_ci      Updated to take advantage of the module spec feature added by
1627db96d56Sopenharmony_ci      :pep:`451`. This allows ``__cached__`` to be set correctly in the
1637db96d56Sopenharmony_ci      case where ``__main__`` is imported from a valid sys.path entry rather
1647db96d56Sopenharmony_ci      than being executed directly.
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci.. seealso::
1677db96d56Sopenharmony_ci
1687db96d56Sopenharmony_ci   :pep:`338` -- Executing modules as scripts
1697db96d56Sopenharmony_ci      PEP written and implemented by Nick Coghlan.
1707db96d56Sopenharmony_ci
1717db96d56Sopenharmony_ci   :pep:`366` -- Main module explicit relative imports
1727db96d56Sopenharmony_ci      PEP written and implemented by Nick Coghlan.
1737db96d56Sopenharmony_ci
1747db96d56Sopenharmony_ci   :pep:`451` -- A ModuleSpec Type for the Import System
1757db96d56Sopenharmony_ci      PEP written and implemented by Eric Snow
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_ci   :ref:`using-on-general` - CPython command line details
1787db96d56Sopenharmony_ci
1797db96d56Sopenharmony_ci   The :func:`importlib.import_module` function
180