blob: c7a7e51971c6ca1c6ca46b9b9dcb8cab13a58375 [file] [log] [blame]
Nick Coghlan98bcb702006-03-24 13:36:33 +00001\section{\module{runpy} ---
2 Locating and executing Python modules.}
3
4\declaremodule{standard}{runpy} % standard library, in Python
5
6\moduleauthor{Nick Coghlan}{ncoghlan@gmail.com}
7
8\modulesynopsis{Locate and execute Python modules as scripts}
9
10\versionadded{2.5}
11
12The \module{runpy} module is used to locate and run Python modules
George Yoshidaf6749392006-04-29 02:43:30 +000013without importing them first. Its main use is to implement the
Nick Coghlan98bcb702006-03-24 13:36:33 +000014\programopt{-m} command line switch that allows scripts to be located
15using the Python module namespace rather than the filesystem.
16
17When executed as a script, the module effectively operates as follows:
18\begin{verbatim}
19 del sys.argv[0] # Remove the runpy module from the arguments
20 run_module(sys.argv[0], run_name="__main__", alter_sys=True)
21\end{verbatim}
22
23The \module{runpy} module provides a single function:
24
25\begin{funcdesc}{run_module}{mod_name\optional{, init_globals}
26\optional{, run_name}\optional{, alter_sys}}
27Execute the code of the specified module and return the resulting
28module globals dictionary. The module's code is first located using
29the standard import mechanism (refer to PEP 302 for details) and
30then executed in a fresh module namespace.
31
32The optional dictionary argument \var{init_globals} may be used to
33pre-populate the globals dictionary before the code is executed.
34The supplied dictionary will not be modified. If any of the special
35global variables below are defined in the supplied dictionary, those
36definitions are overridden by the \code{run_module} function.
37
38The special global variables \code{__name__}, \code{__file__},
39\code{__loader__} and \code{__builtins__} are set in the globals
40dictionary before the module code is executed.
41
42\code{__name__} is set to \var{run_name} if this optional argument is
43supplied, and the \var{mod_name} argument otherwise.
44
45\code{__loader__} is set to the PEP 302 module loader used to retrieve
46the code for the module (This loader may be a wrapper around the
47standard import mechanism).
48
49\code{__file__} is set to the name provided by the module loader. If
50the loader does not make filename information available, this
51variable is set to \code{None}.
52
53\code{__builtins__} is automatically initialised with a reference to
54the top level namespace of the \module{__builtin__} module.
55
56If the argument \var{alter_sys} is supplied and evaluates to
57\code{True}, then \code{sys.argv[0]} is updated with the value of
58\code{__file__} and \code{sys.modules[__name__]} is updated with a
59temporary module object for the module being executed. Both
60\code{sys.argv[0]} and \code{sys.modules[__name__]} are restored to
61their original values before the function returns.
62
63Note that this manipulation of \module{sys} is not thread-safe. Other
64threads may see the partially initialised module, as well as the
65altered list of arguments. It is recommended that the \module{sys}
66module be left alone when invoking this function from threaded code.
67\end{funcdesc}
68
69\begin{seealso}
70
71\seepep{338}{Executing modules as scripts}{PEP written and
72implemented by Nick Coghlan.}
73
74\end{seealso}