1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>32.10. py_compile — Compile Python source files — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="32. Python Language Services" href="language.html" />
34
<link rel="next" title="32.11. compileall — Byte-compile Python libraries" href="compileall.html" />
35
<link rel="prev" title="32.9. pyclbr — Python class browser support" href="pyclbr.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="compileall.html" title="32.11. compileall — Byte-compile Python libraries"
55
accesskey="N">next</a> |</li>
57
<a href="pyclbr.html" title="32.9. pyclbr — Python class browser support"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="language.html" accesskey="U">32. Python Language Services</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-py_compile">
78
<span id="py-compile-compile-python-source-files"></span><h1>32.10. <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"><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>
79
<p id="index-0"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/py_compile.py">Lib/py_compile.py</a></p>
80
<hr class="docutils" />
81
<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"><span class="pre">py_compile</span></code></a> module provides a function to generate a byte-code file
82
from a source file, and another function used when the module source file is
83
invoked as a script.</p>
84
<p>Though not often needed, this function can be useful when installing modules for
85
shared use, especially if some of the users may not have permission to write the
86
byte-code cache files in the directory containing the source code.</p>
87
<dl class="exception">
88
<dt id="py_compile.PyCompileError">
89
<em class="property">exception </em><code class="descclassname">py_compile.</code><code class="descname">PyCompileError</code><a class="headerlink" href="#py_compile.PyCompileError" title="Permalink to this definition">¶</a></dt>
90
<dd><p>Exception raised when an error occurs while attempting to compile the file.</p>
94
<dt id="py_compile.compile">
95
<code class="descclassname">py_compile.</code><code class="descname">compile</code><span class="sig-paren">(</span><em>file</em><span class="optional">[</span>, <em>cfile</em><span class="optional">[</span>, <em>dfile</em><span class="optional">[</span>, <em>doraise</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>
96
<dd><p>Compile a source file to byte-code and write out the byte-code cache file. The
97
source code is loaded from the file named <em>file</em>. The byte-code is written to
98
<em>cfile</em>, which defaults to <em>file</em> <code class="docutils literal"><span class="pre">+</span></code> <code class="docutils literal"><span class="pre">'c'</span></code> (<code class="docutils literal"><span class="pre">'o'</span></code> if optimization is
99
enabled in the current interpreter). If <em>dfile</em> is specified, it is used as the
100
name of the source file in error messages instead of <em>file</em>. If <em>doraise</em> is
101
true, a <a class="reference internal" href="#py_compile.PyCompileError" title="py_compile.PyCompileError"><code class="xref py py-exc docutils literal"><span class="pre">PyCompileError</span></code></a> is raised when an error is encountered while
102
compiling <em>file</em>. If <em>doraise</em> is false (the default), an error string is
103
written to <code class="docutils literal"><span class="pre">sys.stderr</span></code>, but no exception is raised.</p>
106
<dl class="function">
107
<dt id="py_compile.main">
108
<code class="descclassname">py_compile.</code><code class="descname">main</code><span class="sig-paren">(</span><span class="optional">[</span><em>args</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#py_compile.main" title="Permalink to this definition">¶</a></dt>
109
<dd><p>Compile several source files. The files named in <em>args</em> (or on the command
110
line, if <em>args</em> is not specified) are compiled and the resulting bytecode is
111
cached in the normal manner. This function does not search a directory
112
structure to locate source files; it only compiles files named explicitly.
113
If <code class="docutils literal"><span class="pre">'-'</span></code> is the only parameter in args, the list of files is taken from
115
<div class="versionchanged">
116
<p><span class="versionmodified">Changed in version 2.7: </span>Added support for <code class="docutils literal"><span class="pre">'-'</span></code>.</p>
120
<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"><span class="pre">main()</span></code></a> is used to compile all the
121
files named on the command line. The exit status is nonzero if one of the files
122
could not be compiled.</p>
123
<div class="versionchanged">
124
<p><span class="versionmodified">Changed in version 2.6: </span>Added the nonzero exit status when module is run as a script.</p>
126
<div class="admonition seealso">
127
<p class="first admonition-title">See also</p>
128
<dl class="last docutils">
129
<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"><span class="pre">compileall</span></code></a></dt>
130
<dd>Utilities to compile all Python source files in a directory tree.</dd>
139
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
140
<div class="sphinxsidebarwrapper">
141
<h4>Previous topic</h4>
142
<p class="topless"><a href="pyclbr.html"
143
title="previous chapter">32.9. <code class="docutils literal"><span class="pre">pyclbr</span></code> — Python class browser support</a></p>
145
<p class="topless"><a href="compileall.html"
146
title="next chapter">32.11. <code class="docutils literal"><span class="pre">compileall</span></code> — Byte-compile Python libraries</a></p>
148
<ul class="this-page-menu">
149
<li><a href="../bugs.html">Report a Bug</a></li>
150
<li><a href="../_sources/library/py_compile.txt"
151
rel="nofollow">Show Source</a></li>
154
<div id="searchbox" style="display: none" role="search">
155
<h3>Quick search</h3>
156
<form class="search" action="../search.html" method="get">
157
<input type="text" name="q" />
158
<input type="submit" value="Go" />
159
<input type="hidden" name="check_keywords" value="yes" />
160
<input type="hidden" name="area" value="default" />
162
<p class="searchtip" style="font-size: 90%">
163
Enter search terms or a module, class or function name.
166
<script type="text/javascript">$('#searchbox').show(0);</script>
169
<div class="clearer"></div>
171
<div class="related" role="navigation" aria-label="related navigation">
174
<li class="right" style="margin-right: 10px">
175
<a href="../genindex.html" title="General Index"
178
<a href="../py-modindex.html" title="Python Module Index"
181
<a href="compileall.html" title="32.11. compileall — Byte-compile Python libraries"
184
<a href="pyclbr.html" title="32.9. pyclbr — Python class browser support"
186
<li><img src="../_static/py.png" alt=""
187
style="vertical-align: middle; margin-top: -1px"/></li>
188
<li><a href="https://www.python.org/">Python</a> »</li>
190
<span class="version_switcher_placeholder">2.7.12</span>
191
<a href="../index.html">Documentation</a> »
194
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
195
<li class="nav-item nav-item-2"><a href="language.html" >32. Python Language Services</a> »</li>
199
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
201
The Python Software Foundation is a non-profit corporation.
202
<a href="https://www.python.org/psf/donations/">Please donate.</a>
204
Last updated on Sep 20, 2016.
205
<a href="../bugs.html">Found a bug</a>?
207
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'