blob: 77ed8cf4a0dfe347381e463e2a2fe811352070cc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`py_compile` --- Compile Python source files
2=================================================
3
4.. module:: py_compile
5 :synopsis: Generate byte-code files from Python source files.
Christian Heimes5b5e81c2007-12-31 16:14:33 +00006.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7.. documentation based on module docstrings
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index:: pair: file; byte-code
10
11The :mod:`py_compile` module provides a function to generate a byte-code file
12from a source file, and another function used when the module source file is
13invoked as a script.
14
15Though not often needed, this function can be useful when installing modules for
16shared use, especially if some of the users may not have permission to write the
17byte-code cache files in the directory containing the source code.
18
19
20.. exception:: PyCompileError
21
22 Exception raised when an error occurs while attempting to compile the file.
23
24
25.. function:: compile(file[, cfile[, dfile[, doraise]]])
26
27 Compile a source file to byte-code and write out the byte-code cache file. The
28 source code is loaded from the file name *file*. The byte-code is written to
29 *cfile*, which defaults to *file* ``+`` ``'c'`` (``'o'`` if optimization is
30 enabled in the current interpreter). If *dfile* is specified, it is used as the
31 name of the source file in error messages instead of *file*. If *doraise* is
32 true, a :exc:`PyCompileError` is raised when an error is encountered while
33 compiling *file*. If *doraise* is false (the default), an error string is
34 written to ``sys.stderr``, but no exception is raised.
35
36
37.. function:: main([args])
38
39 Compile several source files. The files named in *args* (or on the command
40 line, if *args* is not specified) are compiled and the resulting bytecode is
41 cached in the normal manner. This function does not search a directory
42 structure to locate source files; it only compiles files named explicitly.
43
44When this module is run as a script, the :func:`main` is used to compile all the
Christian Heimesdd15f6c2008-03-16 00:07:10 +000045files named on the command line. The exit status is nonzero if one of the files
46could not be compiled.
47
48.. versionchanged:: 2.6
49
50 Added the nonzero exit status.
Georg Brandl116aa622007-08-15 14:28:22 +000051
52
53.. seealso::
54
55 Module :mod:`compileall`
56 Utilities to compile all Python source files in a directory tree.
57