runpy.rst 7.93 KB
Newer Older
1 2 3 4 5 6 7
:mod:`runpy` --- Locating and executing Python modules
======================================================

.. module:: runpy
   :synopsis: Locate and run Python modules without importing them first.
.. moduleauthor:: Nick Coghlan <ncoghlan@gmail.com>

Raymond Hettinger's avatar
Raymond Hettinger committed
8 9 10
**Source code:** :source:`Lib/runpy.py`

--------------
11 12

The :mod:`runpy` module is used to locate and run Python modules without
Nick Coghlan's avatar
Nick Coghlan committed
13 14 15
importing them first. Its main use is to implement the :option:`-m` command
line switch that allows scripts to be located using the Python module
namespace rather than the filesystem.
16

17 18 19 20 21 22 23 24 25
Note that this is *not* a sandbox module - all code is executed in the
current process, and any side effects (such as cached imports of other
modules) will remain in place after the functions have returned.

Furthermore, any functions and classes defined by the executed code are not
guaranteed to work correctly after a :mod:`runpy` function has returned.
If that limitation is not acceptable for a given use case, :mod:`importlib`
is likely to be a more suitable choice than this module.

Nick Coghlan's avatar
Nick Coghlan committed
26
The :mod:`runpy` module provides two functions:
27 28


29
.. function:: run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
30

31 32 33
   .. index::
      module: __main__

Nick Coghlan's avatar
Nick Coghlan committed
34 35
   Execute the code of the specified module and return the resulting module
   globals dictionary. The module's code is first located using the standard
36
   import mechanism (refer to :pep:`302` for details) and then executed in a
Nick Coghlan's avatar
Nick Coghlan committed
37
   fresh module namespace.
38

Nick Coghlan's avatar
Nick Coghlan committed
39 40 41 42
   If the supplied module name refers to a package rather than a normal
   module, then that package is imported and the ``__main__`` submodule within
   that package is then executed and the resulting module globals dictionary
   returned.
43

Nick Coghlan's avatar
Nick Coghlan committed
44 45 46 47 48
   The optional dictionary argument *init_globals* may be used to pre-populate
   the module's globals dictionary before the code is executed. The supplied
   dictionary will not be modified. If any of the special global variables
   below are defined in the supplied dictionary, those definitions are
   overridden by :func:`run_module`.
49

50 51 52 53 54
   The special global variables ``__name__``, ``__spec__``, ``__file__``,
   ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
   dictionary before the module code is executed (Note that this is a
   minimal set of variables - other variables may be set implicitly as an
   interpreter implementation detail).
55

Nick Coghlan's avatar
Nick Coghlan committed
56 57 58
   ``__name__`` is set to *run_name* if this optional argument is not
   :const:`None`, to ``mod_name + '.__main__'`` if the named module is a
   package and to the *mod_name* argument otherwise.
59

60 61 62
   ``__spec__`` will be set appropriately for the *actually* imported
   module (that is, ``__spec__.name`` will always be *mod_name* or
   ``mod_name + '.__main__``, never *run_name*).
63

64 65
   ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` are
   :ref:`set as normal <import-mod-attrs>` based on the module spec.
66

Nick Coghlan's avatar
Nick Coghlan committed
67 68
   If the argument *alter_sys* is supplied and evaluates to :const:`True`,
   then ``sys.argv[0]`` is updated with the value of ``__file__`` and
69 70 71
   ``sys.modules[__name__]`` is updated with a temporary module object for the
   module being executed. Both ``sys.argv[0]`` and ``sys.modules[__name__]``
   are restored to their original values before the function returns.
72

Nick Coghlan's avatar
Nick Coghlan committed
73 74 75 76
   Note that this manipulation of :mod:`sys` is not thread-safe. Other threads
   may see the partially initialised module, as well as the altered list of
   arguments. It is recommended that the :mod:`sys` module be left alone when
   invoking this function from threaded code.
77 78


79
   .. versionchanged:: 3.1
80
      Added ability to execute packages by looking for a ``__main__`` submodule.
Nick Coghlan's avatar
Nick Coghlan committed
81

82
   .. versionchanged:: 3.2
83
      Added ``__cached__`` global variable (see :pep:`3147`).
84

85 86 87 88 89
   .. versionchanged:: 3.4
      Updated to take advantage of the module spec feature added by
      :pep:`451`. This allows ``__cached__`` to be set correctly for modules
      run this way, as well as ensuring the real module name is always
      accessible as ``__spec__.name``.
Nick Coghlan's avatar
Nick Coghlan committed
90 91 92

.. function:: run_path(file_path, init_globals=None, run_name=None)

93 94 95
   .. index::
      module: __main__

Nick Coghlan's avatar
Nick Coghlan committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
   Execute the code at the named filesystem location and return the resulting
   module globals dictionary. As with a script name supplied to the CPython
   command line, the supplied path may refer to a Python source file, a
   compiled bytecode file or a valid sys.path entry containing a ``__main__``
   module (e.g. a zipfile containing a top-level ``__main__.py`` file).

   For a simple script, the specified code is simply executed in a fresh
   module namespace. For a valid sys.path entry (typically a zipfile or
   directory), the entry is first added to the beginning of ``sys.path``. The
   function then looks for and executes a :mod:`__main__` module using the
   updated path. Note that there is no special protection against invoking
   an existing :mod:`__main__` entry located elsewhere on ``sys.path`` if
   there is no such module at the specified location.

   The optional dictionary argument *init_globals* may be used to pre-populate
   the module's globals dictionary before the code is executed. The supplied
   dictionary will not be modified. If any of the special global variables
   below are defined in the supplied dictionary, those definitions are
   overridden by :func:`run_path`.

116 117 118 119 120
   The special global variables ``__name__``, ``__spec__``, ``__file__``,
   ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
   dictionary before the module code is executed (Note that this is a
   minimal set of variables - other variables may be set implicitly as an
   interpreter implementation detail).
Nick Coghlan's avatar
Nick Coghlan committed
121 122 123 124

   ``__name__`` is set to *run_name* if this optional argument is not
   :const:`None` and to ``'<run_path>'`` otherwise.

125 126 127 128
   If the supplied path directly references a script file (whether as source
   or as precompiled byte code), then ``__file__`` will be set to the
   supplied path, and ``__spec__``, ``__cached__``, ``__loader__`` and
   ``__package__`` will all be set to :const:`None`.
Nick Coghlan's avatar
Nick Coghlan committed
129

130 131
   ``__spec__`` will be set to :const:`None` if the supplied path is a
   direct path to a script (as source or as precompiled bytecode).
Nick Coghlan's avatar
Nick Coghlan committed
132

133 134 135 136 137
   If the supplied path is a reference to a valid sys.path entry, then
   ``__spec__`` will be set appropriately for the imported ``__main__``
   module (that is, ``__spec__.name`` will always be ``__main__``).
   ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
   :ref:`set as normal <import-mod-attrs>` based on the module spec.
Nick Coghlan's avatar
Nick Coghlan committed
138 139 140 141 142 143 144 145 146 147

   A number of alterations are also made to the :mod:`sys` module. Firstly,
   ``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated
   with the value of ``file_path`` and ``sys.modules[__name__]`` is updated
   with a temporary module object for the module being executed. All
   modifications to items in :mod:`sys` are reverted before the function
   returns.

   Note that, unlike :func:`run_module`, the alterations made to :mod:`sys`
   are not optional in this function as these adjustments are essential to
148
   allowing the execution of sys.path entries. As the thread-safety
Nick Coghlan's avatar
Nick Coghlan committed
149 150
   limitations still apply, use of this function in threaded code should be
   either serialised with the import lock or delegated to a separate process.
151

Nick Coghlan's avatar
Nick Coghlan committed
152
   .. versionadded:: 3.2
153

154 155 156 157 158 159
   .. versionchanged:: 3.4
      Updated to take advantage of the module spec feature added by
      :pep:`451`. This allows ``__cached__`` to be set correctly in the
      case where ``__main__`` is imported from a valid sys.path entry rather
      than being executed directly.

160 161 162
.. seealso::

   :pep:`338` - Executing modules as scripts
163 164 165 166
      PEP written and implemented by Nick Coghlan.

   :pep:`366` - Main module explicit relative imports
      PEP written and implemented by Nick Coghlan.
167

168 169 170
   :pep:`451` - A ModuleSpec Type for the Import System
      PEP written and implemented by Eric Snow

Nick Coghlan's avatar
Nick Coghlan committed
171
   :ref:`using-on-general` - CPython command line details
172 173

   The :func:`importlib.import_module` function