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