0x1949 Team - FAZEMRX - MANAGER
Edit File: py_compile.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>32.10. py_compile — Compile Python source files — Python 2.7.18 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/classic.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.18 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="32.11. compileall — Byte-compile Python libraries" href="compileall.html" /> <link rel="prev" title="32.9. pyclbr — Python class browser support" href="pyclbr.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/py_compile.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;"> This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>. You should install the python3 and python3-doc packages and read the <a href="file:///usr/share/doc/python3-doc/html/library/py_compile.html"> Python documentation for the Python3 version packaged in this release</a>. </div> <div class="related" role="navigation" aria-label="related navigation"> <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="compileall.html" title="32.11. compileall — Byte-compile Python libraries" accesskey="N">next</a> |</li> <li class="right" > <a href="pyclbr.html" title="32.9. pyclbr — Python class browser support" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="language.html" accesskey="U"><span class="section-number">32. </span>Python Language Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">32.10. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code> — Compile Python source files</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-py_compile"> <span id="py-compile-compile-python-source-files"></span><h1><span class="section-number">32.10. </span><a class="reference internal" href="#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> — Compile Python source files<a class="headerlink" href="#module-py_compile" title="Permalink to this headline">¶</a></h1> <p id="index-0"><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/2.7/Lib/py_compile.py">Lib/py_compile.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.</p> <p>Though not often needed, this function can be useful when installing modules for shared use, especially if some of the users may not have permission to write the byte-code cache files in the directory containing the source code.</p> <dl class="py exception"> <dt class="sig sig-object py" id="py_compile.PyCompileError"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">py_compile.</span></span><span class="sig-name descname"><span class="pre">PyCompileError</span></span><a class="headerlink" href="#py_compile.PyCompileError" title="Permalink to this definition">¶</a></dt> <dd><p>Exception raised when an error occurs while attempting to compile the file.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="py_compile.compile"> <span class="sig-prename descclassname"><span class="pre">py_compile.</span></span><span class="sig-name descname"><span class="pre">compile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">cfile</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">dfile</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">doraise</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#py_compile.compile" title="Permalink to this definition">¶</a></dt> <dd><p>Compile a source file to byte-code and write out the byte-code cache file. The source code is loaded from the file named <em>file</em>. The byte-code is written to <em>cfile</em>, which defaults to <em>file</em> <code class="docutils literal notranslate"><span class="pre">+</span></code> <code class="docutils literal notranslate"><span class="pre">'c'</span></code> (<code class="docutils literal notranslate"><span class="pre">'o'</span></code> if optimization is enabled in the current interpreter). If <em>dfile</em> is specified, it is used as the name of the source file in error messages instead of <em>file</em>. If <em>doraise</em> is true, a <a class="reference internal" href="#py_compile.PyCompileError" title="py_compile.PyCompileError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PyCompileError</span></code></a> is raised when an error is encountered while compiling <em>file</em>. If <em>doraise</em> is false (the default), an error string is written to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>, but no exception is raised.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="py_compile.main"> <span class="sig-prename descclassname"><span class="pre">py_compile.</span></span><span class="sig-name descname"><span class="pre">main</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#py_compile.main" title="Permalink to this definition">¶</a></dt> <dd><p>Compile several source files. The files named in <em>args</em> (or on the command line, if <em>args</em> is not specified) are compiled and the resulting bytecode is cached in the normal manner. This function does not search a directory structure to locate source files; it only compiles files named explicitly. If <code class="docutils literal notranslate"><span class="pre">'-'</span></code> is the only parameter in args, the list of files is taken from standard input.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.7: </span>Added support for <code class="docutils literal notranslate"><span class="pre">'-'</span></code>.</p> </div> </dd></dl> <p>When this module is run as a script, the <a class="reference internal" href="#py_compile.main" title="py_compile.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> is used to compile all the files named on the command line. The exit status is nonzero if one of the files could not be compiled.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.6: </span>Added the nonzero exit status when module is run as a script.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>Module <a class="reference internal" href="compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a></dt><dd><p>Utilities to compile all Python source files in a directory tree.</p> </dd> </dl> </div> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="pyclbr.html" title="previous chapter"><span class="section-number">32.9. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">pyclbr</span></code> — Python class browser support</a></p> <h4>Next topic</h4> <p class="topless"><a href="compileall.html" title="next chapter"><span class="section-number">32.11. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code> — Byte-compile Python libraries</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/py_compile.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <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="compileall.html" title="32.11. compileall — Byte-compile Python libraries" >next</a> |</li> <li class="right" > <a href="pyclbr.html" title="32.9. pyclbr — Python class browser support" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="language.html" ><span class="section-number">32. </span>Python Language Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">32.10. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code> — Compile Python source files</a></li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on November 21, 2024. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 4.3.2. </div> </body> </html>