blob: 0bcfad2e237db92f3f07fa40c0aa27b3d9f6a626 [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
Nick Coghlan999a3362006-06-28 10:41:47 +000038The special global variables \code{__name__}, \code{__module_name__},
39\code{__file__}, \code{__loader__} and \code{__builtins__} are
40set in the globals dictionary before the module code is executed.
Nick Coghlan98bcb702006-03-24 13:36:33 +000041
42\code{__name__} is set to \var{run_name} if this optional argument is
43supplied, and the \var{mod_name} argument otherwise.
44
Nick Coghlan999a3362006-06-28 10:41:47 +000045\code{__module_name__} is always set to \var{mod_name} (this allows
46modules to use imports relative to their package name).
47
Nick Coghlan98bcb702006-03-24 13:36:33 +000048\code{__loader__} is set to the PEP 302 module loader used to retrieve
Nick Coghlan999a3362006-06-28 10:41:47 +000049the code for the module (This will not be defined if the module was
50found using the standard import mechanism).
Nick Coghlan98bcb702006-03-24 13:36:33 +000051
52\code{__file__} is set to the name provided by the module loader. If
53the loader does not make filename information available, this
54variable is set to \code{None}.
55
56\code{__builtins__} is automatically initialised with a reference to
57the top level namespace of the \module{__builtin__} module.
58
59If the argument \var{alter_sys} is supplied and evaluates to
60\code{True}, then \code{sys.argv[0]} is updated with the value of
Nick Coghlan999a3362006-06-28 10:41:47 +000061\code{__file__} and \code{sys.modules[mod_name]} is updated with a
Nick Coghlan98bcb702006-03-24 13:36:33 +000062temporary module object for the module being executed. Both
Nick Coghlan999a3362006-06-28 10:41:47 +000063\code{sys.argv[0]} and \code{sys.modules[mod_name]} are restored to
64their original values before the function returns. If \var{run_name}
65differs from \var{mod_name} entries are made in \code{sys.modules}
66for both names.
Nick Coghlan98bcb702006-03-24 13:36:33 +000067
68Note that this manipulation of \module{sys} is not thread-safe. Other
69threads may see the partially initialised module, as well as the
70altered list of arguments. It is recommended that the \module{sys}
71module be left alone when invoking this function from threaded code.
72\end{funcdesc}
73
74\begin{seealso}
75
76\seepep{338}{Executing modules as scripts}{PEP written and
77implemented by Nick Coghlan.}
78
79\end{seealso}