blob: 78d01432a2767d5b457b59a1faa31b95dcb558b4 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`runpy` --- Locating and executing Python modules
2======================================================
3
4.. module:: runpy
5 :synopsis: Locate and run Python modules without importing them first.
6.. moduleauthor:: Nick Coghlan <ncoghlan@gmail.com>
7
Raymond Hettingera1993682011-01-27 01:20:32 +00008**Source code:** :source:`Lib/runpy.py`
9
10--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandl116aa622007-08-15 14:28:22 +000012The :mod:`runpy` module is used to locate and run Python modules without
Nick Coghlan260bd3e2009-11-16 06:49:25 +000013importing them first. Its main use is to implement the :option:`-m` command
14line switch that allows scripts to be located using the Python module
15namespace rather than the filesystem.
Georg Brandl116aa622007-08-15 14:28:22 +000016
Nick Coghlana3d1cac2012-07-15 00:36:39 +100017Note that this is *not* a sandbox module - all code is executed in the
18current process, and any side effects (such as cached imports of other
19modules) will remain in place after the functions have returned.
20
21Furthermore, any functions and classes defined by the executed code are not
22guaranteed to work correctly after a :mod:`runpy` function has returned.
23If that limitation is not acceptable for a given use case, :mod:`importlib`
24is likely to be a more suitable choice than this module.
25
Nick Coghlan260bd3e2009-11-16 06:49:25 +000026The :mod:`runpy` module provides two functions:
Georg Brandl116aa622007-08-15 14:28:22 +000027
28
Georg Brandl18244152009-09-02 20:34:52 +000029.. function:: run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
Georg Brandl116aa622007-08-15 14:28:22 +000030
Senthil Kumaran4932e142014-06-20 01:37:53 -070031 .. index::
32 module: __main__
33
Nick Coghlan260bd3e2009-11-16 06:49:25 +000034 Execute the code of the specified module and return the resulting module
35 globals dictionary. The module's code is first located using the standard
Victor Stinner766ad362010-05-14 14:36:18 +000036 import mechanism (refer to :pep:`302` for details) and then executed in a
Nick Coghlan260bd3e2009-11-16 06:49:25 +000037 fresh module namespace.
Georg Brandl116aa622007-08-15 14:28:22 +000038
Martin Panter7dda4212015-12-10 06:47:06 +000039 The *mod_name* argument should be an absolute module name.
40 If the module name refers to a package rather than a normal
Nick Coghlan260bd3e2009-11-16 06:49:25 +000041 module, then that package is imported and the ``__main__`` submodule within
42 that package is then executed and the resulting module globals dictionary
43 returned.
Nick Coghlan3f48ae32009-02-08 01:58:26 +000044
Nick Coghlan260bd3e2009-11-16 06:49:25 +000045 The optional dictionary argument *init_globals* may be used to pre-populate
46 the module's globals dictionary before the code is executed. The supplied
47 dictionary will not be modified. If any of the special global variables
48 below are defined in the supplied dictionary, those definitions are
49 overridden by :func:`run_module`.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Nick Coghlan720c7e22013-12-15 20:33:02 +100051 The special global variables ``__name__``, ``__spec__``, ``__file__``,
52 ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
53 dictionary before the module code is executed (Note that this is a
54 minimal set of variables - other variables may be set implicitly as an
55 interpreter implementation detail).
Georg Brandl116aa622007-08-15 14:28:22 +000056
Nick Coghlan260bd3e2009-11-16 06:49:25 +000057 ``__name__`` is set to *run_name* if this optional argument is not
58 :const:`None`, to ``mod_name + '.__main__'`` if the named module is a
59 package and to the *mod_name* argument otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +000060
Nick Coghlan720c7e22013-12-15 20:33:02 +100061 ``__spec__`` will be set appropriately for the *actually* imported
62 module (that is, ``__spec__.name`` will always be *mod_name* or
63 ``mod_name + '.__main__``, never *run_name*).
Georg Brandl116aa622007-08-15 14:28:22 +000064
Nick Coghlan720c7e22013-12-15 20:33:02 +100065 ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` are
66 :ref:`set as normal <import-mod-attrs>` based on the module spec.
Georg Brandl116aa622007-08-15 14:28:22 +000067
Nick Coghlan260bd3e2009-11-16 06:49:25 +000068 If the argument *alter_sys* is supplied and evaluates to :const:`True`,
69 then ``sys.argv[0]`` is updated with the value of ``__file__`` and
Thomas Woutersed03b412007-08-28 21:37:11 +000070 ``sys.modules[__name__]`` is updated with a temporary module object for the
71 module being executed. Both ``sys.argv[0]`` and ``sys.modules[__name__]``
72 are restored to their original values before the function returns.
Georg Brandl116aa622007-08-15 14:28:22 +000073
Nick Coghlan260bd3e2009-11-16 06:49:25 +000074 Note that this manipulation of :mod:`sys` is not thread-safe. Other threads
75 may see the partially initialised module, as well as the altered list of
76 arguments. It is recommended that the :mod:`sys` module be left alone when
77 invoking this function from threaded code.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Berker Peksag8b1cbd22014-12-10 01:47:02 +020079 .. seealso::
80 The :option:`-m` option offering equivalent functionality from the
81 command line.
Georg Brandl116aa622007-08-15 14:28:22 +000082
Nick Coghlan3f48ae32009-02-08 01:58:26 +000083 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +000084 Added ability to execute packages by looking for a ``__main__`` submodule.
Nick Coghlan3f48ae32009-02-08 01:58:26 +000085
Éric Araujo930df312010-12-16 06:28:48 +000086 .. versionchanged:: 3.2
Nick Coghlan720c7e22013-12-15 20:33:02 +100087 Added ``__cached__`` global variable (see :pep:`3147`).
Éric Araujo930df312010-12-16 06:28:48 +000088
Nick Coghlan720c7e22013-12-15 20:33:02 +100089 .. versionchanged:: 3.4
90 Updated to take advantage of the module spec feature added by
91 :pep:`451`. This allows ``__cached__`` to be set correctly for modules
92 run this way, as well as ensuring the real module name is always
93 accessible as ``__spec__.name``.
Nick Coghlan3f48ae32009-02-08 01:58:26 +000094
Nick Coghlan260bd3e2009-11-16 06:49:25 +000095.. function:: run_path(file_path, init_globals=None, run_name=None)
96
Senthil Kumaran4932e142014-06-20 01:37:53 -070097 .. index::
98 module: __main__
99
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000100 Execute the code at the named filesystem location and return the resulting
101 module globals dictionary. As with a script name supplied to the CPython
102 command line, the supplied path may refer to a Python source file, a
103 compiled bytecode file or a valid sys.path entry containing a ``__main__``
104 module (e.g. a zipfile containing a top-level ``__main__.py`` file).
105
106 For a simple script, the specified code is simply executed in a fresh
107 module namespace. For a valid sys.path entry (typically a zipfile or
108 directory), the entry is first added to the beginning of ``sys.path``. The
109 function then looks for and executes a :mod:`__main__` module using the
110 updated path. Note that there is no special protection against invoking
111 an existing :mod:`__main__` entry located elsewhere on ``sys.path`` if
112 there is no such module at the specified location.
113
114 The optional dictionary argument *init_globals* may be used to pre-populate
115 the module's globals dictionary before the code is executed. The supplied
116 dictionary will not be modified. If any of the special global variables
117 below are defined in the supplied dictionary, those definitions are
118 overridden by :func:`run_path`.
119
Nick Coghlan720c7e22013-12-15 20:33:02 +1000120 The special global variables ``__name__``, ``__spec__``, ``__file__``,
121 ``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
122 dictionary before the module code is executed (Note that this is a
123 minimal set of variables - other variables may be set implicitly as an
124 interpreter implementation detail).
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000125
126 ``__name__`` is set to *run_name* if this optional argument is not
127 :const:`None` and to ``'<run_path>'`` otherwise.
128
Nick Coghlan720c7e22013-12-15 20:33:02 +1000129 If the supplied path directly references a script file (whether as source
130 or as precompiled byte code), then ``__file__`` will be set to the
131 supplied path, and ``__spec__``, ``__cached__``, ``__loader__`` and
132 ``__package__`` will all be set to :const:`None`.
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000133
Nick Coghlan720c7e22013-12-15 20:33:02 +1000134 If the supplied path is a reference to a valid sys.path entry, then
135 ``__spec__`` will be set appropriately for the imported ``__main__``
136 module (that is, ``__spec__.name`` will always be ``__main__``).
137 ``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
138 :ref:`set as normal <import-mod-attrs>` based on the module spec.
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000139
140 A number of alterations are also made to the :mod:`sys` module. Firstly,
141 ``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated
142 with the value of ``file_path`` and ``sys.modules[__name__]`` is updated
143 with a temporary module object for the module being executed. All
144 modifications to items in :mod:`sys` are reverted before the function
145 returns.
146
147 Note that, unlike :func:`run_module`, the alterations made to :mod:`sys`
148 are not optional in this function as these adjustments are essential to
Georg Brandlf285bcc2010-10-19 21:07:16 +0000149 allowing the execution of sys.path entries. As the thread-safety
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000150 limitations still apply, use of this function in threaded code should be
151 either serialised with the import lock or delegated to a separate process.
152
Berker Peksag8b1cbd22014-12-10 01:47:02 +0200153 .. seealso::
154 :ref:`using-on-interface-options` for equivalent functionality on the
155 command line (``python path/to/script``).
156
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000157 .. versionadded:: 3.2
158
Nick Coghlan720c7e22013-12-15 20:33:02 +1000159 .. versionchanged:: 3.4
160 Updated to take advantage of the module spec feature added by
161 :pep:`451`. This allows ``__cached__`` to be set correctly in the
162 case where ``__main__`` is imported from a valid sys.path entry rather
163 than being executed directly.
164
Georg Brandl116aa622007-08-15 14:28:22 +0000165.. seealso::
166
Berker Peksag8b1cbd22014-12-10 01:47:02 +0200167 :pep:`338` -- Executing modules as scripts
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000168 PEP written and implemented by Nick Coghlan.
169
Berker Peksag8b1cbd22014-12-10 01:47:02 +0200170 :pep:`366` -- Main module explicit relative imports
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000171 PEP written and implemented by Nick Coghlan.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Berker Peksag8b1cbd22014-12-10 01:47:02 +0200173 :pep:`451` -- A ModuleSpec Type for the Import System
Nick Coghlan720c7e22013-12-15 20:33:02 +1000174 PEP written and implemented by Eric Snow
175
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000176 :ref:`using-on-general` - CPython command line details
Nick Coghlana3d1cac2012-07-15 00:36:39 +1000177
178 The :func:`importlib.import_module` function