1 | \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 |
|
---|
12 | The \module{runpy} module is used to locate and run Python modules
|
---|
13 | without importing them first. Its main use is to implement the
|
---|
14 | \programopt{-m} command line switch that allows scripts to be located
|
---|
15 | using the Python module namespace rather than the filesystem.
|
---|
16 |
|
---|
17 | When 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 |
|
---|
23 | The \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}}
|
---|
27 | Execute the code of the specified module and return the resulting
|
---|
28 | module globals dictionary. The module's code is first located using
|
---|
29 | the standard import mechanism (refer to PEP 302 for details) and
|
---|
30 | then executed in a fresh module namespace.
|
---|
31 |
|
---|
32 | The optional dictionary argument \var{init_globals} may be used to
|
---|
33 | pre-populate the globals dictionary before the code is executed.
|
---|
34 | The supplied dictionary will not be modified. If any of the special
|
---|
35 | global variables below are defined in the supplied dictionary, those
|
---|
36 | definitions are overridden by the \code{run_module} function.
|
---|
37 |
|
---|
38 | The special global variables \code{__name__}, \code{__file__},
|
---|
39 | \code{__loader__} and \code{__builtins__} are set in the globals
|
---|
40 | dictionary before the module code is executed.
|
---|
41 |
|
---|
42 | \code{__name__} is set to \var{run_name} if this optional argument is
|
---|
43 | supplied, and the \var{mod_name} argument otherwise.
|
---|
44 |
|
---|
45 | \code{__loader__} is set to the PEP 302 module loader used to retrieve
|
---|
46 | the code for the module (This loader may be a wrapper around the
|
---|
47 | standard import mechanism).
|
---|
48 |
|
---|
49 | \code{__file__} is set to the name provided by the module loader. If
|
---|
50 | the loader does not make filename information available, this
|
---|
51 | variable is set to \code{None}.
|
---|
52 |
|
---|
53 | \code{__builtins__} is automatically initialised with a reference to
|
---|
54 | the top level namespace of the \module{__builtin__} module.
|
---|
55 |
|
---|
56 | If 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
|
---|
59 | temporary module object for the module being executed. Both
|
---|
60 | \code{sys.argv[0]} and \code{sys.modules[__name__]} are restored to
|
---|
61 | their original values before the function returns.
|
---|
62 |
|
---|
63 | Note that this manipulation of \module{sys} is not thread-safe. Other
|
---|
64 | threads may see the partially initialised module, as well as the
|
---|
65 | altered list of arguments. It is recommended that the \module{sys}
|
---|
66 | module 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
|
---|
72 | implemented by Nick Coghlan.}
|
---|
73 |
|
---|
74 | \end{seealso}
|
---|