korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>30.7. runpy — Locating and executing Python modules — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="30. Importing Modules" href="modules.html" /> <link rel="next" title="31. Python Language Services" href="language.html" /> <link rel="prev" title="30.6. modulefinder — Find modules used by a script" href="modulefinder.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="language.html" title="31. Python Language Services" accesskey="N">next</a> |</li> <li class="right" > <a href="modulefinder.html" title="30.6. modulefinder — Find modules used by a script" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="modules.html" accesskey="U">30. Importing Modules</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-runpy"> <span id="runpy-locating-and-executing-python-modules"></span><h1>30.7. <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> — Locating and executing Python modules<a class="headerlink" href="#module-runpy" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/runpy.py">Lib/runpy.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> module is used to locate and run Python modules without importing them first. Its main use is to implement the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><em class="xref std std-option">-m</em></a> command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.</p> <p>The <a class="reference internal" href="#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> module provides two functions:</p> <dl class="function"> <dt id="runpy.run_module"> <tt class="descclassname">runpy.</tt><tt class="descname">run_module</tt><big>(</big><em>mod_name</em>, <em>init_globals=None</em>, <em>run_name=None</em>, <em>alter_sys=False</em><big>)</big><a class="headerlink" href="#runpy.run_module" title="Permalink to this definition">¶</a></dt> <dd><p>Execute the code of the specified module and return the resulting module globals dictionary. The module’s code is first located using the standard import mechanism (refer to <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for details) and then executed in a fresh module namespace.</p> <p>If the supplied module name refers to a package rather than a normal module, then that package is imported and the <tt class="docutils literal"><span class="pre">__main__</span></tt> submodule within that package is then executed and the resulting module globals dictionary returned.</p> <p>The optional dictionary argument <em>init_globals</em> 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 <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><tt class="xref py py-func docutils literal"><span class="pre">run_module()</span></tt></a>.</p> <p>The special global variables <tt class="docutils literal"><span class="pre">__name__</span></tt>, <tt class="docutils literal"><span class="pre">__file__</span></tt>, <tt class="docutils literal"><span class="pre">__loader__</span></tt> and <tt class="docutils literal"><span class="pre">__package__</span></tt> 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).</p> <p><tt class="docutils literal"><span class="pre">__name__</span></tt> is set to <em>run_name</em> if this optional argument is not <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>, to <tt class="docutils literal"><span class="pre">mod_name</span> <span class="pre">+</span> <span class="pre">'.__main__'</span></tt> if the named module is a package and to the <em>mod_name</em> argument otherwise.</p> <p><tt class="docutils literal"><span class="pre">__file__</span></tt> is set to the name provided by the module loader. If the loader does not make filename information available, this variable is set to <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>.</p> <p><tt class="docutils literal"><span class="pre">__loader__</span></tt> is set to the <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> module loader used to retrieve the code for the module (This loader may be a wrapper around the standard import mechanism).</p> <p><tt class="docutils literal"><span class="pre">__package__</span></tt> is set to <em>mod_name</em> if the named module is a package and to <tt class="docutils literal"><span class="pre">mod_name.rpartition('.')[0]</span></tt> otherwise.</p> <p>If the argument <em>alter_sys</em> is supplied and evaluates to <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, then <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is updated with the value of <tt class="docutils literal"><span class="pre">__file__</span></tt> and <tt class="docutils literal"><span class="pre">sys.modules[__name__]</span></tt> is updated with a temporary module object for the module being executed. Both <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> and <tt class="docutils literal"><span class="pre">sys.modules[__name__]</span></tt> are restored to their original values before the function returns.</p> <p>Note that this manipulation of <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> 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 <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> module be left alone when invoking this function from threaded code.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.7: </span>Added ability to execute packages by looking for a <tt class="docutils literal"><span class="pre">__main__</span></tt> submodule</p> </dd></dl> <dl class="function"> <dt id="runpy.run_path"> <tt class="descclassname">runpy.</tt><tt class="descname">run_path</tt><big>(</big><em>file_path</em>, <em>init_globals=None</em>, <em>run_name=None</em><big>)</big><a class="headerlink" href="#runpy.run_path" title="Permalink to this definition">¶</a></dt> <dd><p>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 <tt class="docutils literal"><span class="pre">__main__</span></tt> module (e.g. a zipfile containing a top-level <tt class="docutils literal"><span class="pre">__main__.py</span></tt> file).</p> <p>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 <tt class="docutils literal"><span class="pre">sys.path</span></tt>. The function then looks for and executes a <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><tt class="xref py py-mod docutils literal"><span class="pre">__main__</span></tt></a> module using the updated path. Note that there is no special protection against invoking an existing <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><tt class="xref py py-mod docutils literal"><span class="pre">__main__</span></tt></a> entry located elsewhere on <tt class="docutils literal"><span class="pre">sys.path</span></tt> if there is no such module at the specified location.</p> <p>The optional dictionary argument <em>init_globals</em> 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 <a class="reference internal" href="#runpy.run_path" title="runpy.run_path"><tt class="xref py py-func docutils literal"><span class="pre">run_path()</span></tt></a>.</p> <p>The special global variables <tt class="docutils literal"><span class="pre">__name__</span></tt>, <tt class="docutils literal"><span class="pre">__file__</span></tt>, <tt class="docutils literal"><span class="pre">__loader__</span></tt> and <tt class="docutils literal"><span class="pre">__package__</span></tt> 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).</p> <p><tt class="docutils literal"><span class="pre">__name__</span></tt> is set to <em>run_name</em> if this optional argument is not <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a> and to <tt class="docutils literal"><span class="pre">'<run_path>'</span></tt> otherwise.</p> <p><tt class="docutils literal"><span class="pre">__file__</span></tt> is set to the name provided by the module loader. If the loader does not make filename information available, this variable is set to <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>. For a simple script, this will be set to <tt class="docutils literal"><span class="pre">file_path</span></tt>.</p> <p><tt class="docutils literal"><span class="pre">__loader__</span></tt> is set to the <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> module loader used to retrieve the code for the module (This loader may be a wrapper around the standard import mechanism). For a simple script, this will be set to <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>.</p> <p><tt class="docutils literal"><span class="pre">__package__</span></tt> is set to <tt class="docutils literal"><span class="pre">__name__.rpartition('.')[0]</span></tt>.</p> <p>A number of alterations are also made to the <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> module. Firstly, <tt class="docutils literal"><span class="pre">sys.path</span></tt> may be altered as described above. <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> is updated with the value of <tt class="docutils literal"><span class="pre">file_path</span></tt> and <tt class="docutils literal"><span class="pre">sys.modules[__name__]</span></tt> is updated with a temporary module object for the module being executed. All modifications to items in <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> are reverted before the function returns.</p> <p>Note that, unlike <a class="reference internal" href="#runpy.run_module" title="runpy.run_module"><tt class="xref py py-func docutils literal"><span class="pre">run_module()</span></tt></a>, the alterations made to <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> are not optional in this function as these adjustments are essential to allowing the execution of sys.path entries. As the thread-safety limitations still apply, use of this function in threaded code should be either serialised with the import lock or delegated to a separate process.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="docutils"> <dt><span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0338"><strong>PEP 338</strong></a> - Executing modules as scripts</dt> <dd>PEP written and implemented by Nick Coghlan.</dd> <dt><span class="target" id="index-4"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0366"><strong>PEP 366</strong></a> - Main module explicit relative imports</dt> <dd>PEP written and implemented by Nick Coghlan.</dd> </dl> <p class="last"><a class="reference internal" href="../using/cmdline.html#using-on-general"><em>Command line and environment</em></a> - CPython command line details</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="modulefinder.html" title="previous chapter">30.6. <tt class="docutils literal"><span class="pre">modulefinder</span></tt> — Find modules used by a script</a></p> <h4>Next topic</h4> <p class="topless"><a href="language.html" title="next chapter">31. Python Language Services</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/runpy.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="language.html" title="31. Python Language Services" >next</a> |</li> <li class="right" > <a href="modulefinder.html" title="30.6. modulefinder — Find modules used by a script" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="modules.html" >30. Importing Modules</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>