blob: 07ddc25422c6bca0fd156bf9b66db4de10021dfa [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
Raymond Hettinger469271d2011-01-27 20:38:46 +000011**Source code:** :source:`Lib/py_compile.py`
12
13--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015The :mod:`py_compile` module provides a function to generate a byte-code file
16from a source file, and another function used when the module source file is
17invoked as a script.
18
19Though not often needed, this function can be useful when installing modules for
20shared use, especially if some of the users may not have permission to write the
21byte-code cache files in the directory containing the source code.
22
23
24.. exception:: PyCompileError
25
26 Exception raised when an error occurs while attempting to compile the file.
27
28
Georg Brandl8334fd92010-12-04 10:26:46 +000029.. function:: compile(file, cfile=None, dfile=None, doraise=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000030
Éric Araujo930df312010-12-16 06:28:48 +000031 Compile a source file to byte-code and write out the byte-code cache file.
32 The source code is loaded from the file name *file*. The byte-code is
33 written to *cfile*, which defaults to the :PEP:`3147` path, ending in
34 ``.pyc`` (``.pyo`` if optimization is enabled in the current interpreter).
35 For example, if *file* is ``/foo/bar/baz.py`` *cfile* will default to
36 ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. If *dfile* is
37 specified, it is used as the name of the source file in error messages when
38 instead of *file*. If *doraise* is true, a :exc:`PyCompileError` is raised
39 when an error is encountered while compiling *file*. If *doraise* is false
40 (the default), an error string is written to ``sys.stderr``, but no exception
41 is raised. This function returns the path to byte-compiled file, i.e.
42 whatever *cfile* value was used.
Georg Brandl116aa622007-08-15 14:28:22 +000043
Georg Brandl8334fd92010-12-04 10:26:46 +000044 *optimize* controls the optimization level and is passed to the built-in
45 :func:`compile` function. The default of ``-1`` selects the optimization
46 level of the current interpreter.
47
48 .. versionchanged:: 3.2
Éric Araujo930df312010-12-16 06:28:48 +000049 Changed default value of *cfile* to be :PEP:`3147`-compliant. Previous
50 default was *file* + ``'c'`` (``'o'`` if optimization was enabled).
51 Also added the *optimize* parameter.
Georg Brandl8334fd92010-12-04 10:26:46 +000052
Georg Brandl116aa622007-08-15 14:28:22 +000053
Georg Brandl18244152009-09-02 20:34:52 +000054.. function:: main(args=None)
Georg Brandl116aa622007-08-15 14:28:22 +000055
56 Compile several source files. The files named in *args* (or on the command
Georg Brandl18244152009-09-02 20:34:52 +000057 line, if *args* is ``None``) are compiled and the resulting bytecode is
Georg Brandl116aa622007-08-15 14:28:22 +000058 cached in the normal manner. This function does not search a directory
59 structure to locate source files; it only compiles files named explicitly.
Éric Araujo930df312010-12-16 06:28:48 +000060 If ``'-'`` is the only parameter in args, the list of files is taken from
61 standard input.
62
63 .. versionchanged:: 3.2
64 Added support for ``'-'``.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66When this module is run as a script, the :func:`main` is used to compile all the
Christian Heimesdd15f6c2008-03-16 00:07:10 +000067files named on the command line. The exit status is nonzero if one of the files
68could not be compiled.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
71.. seealso::
72
73 Module :mod:`compileall`
74 Utilities to compile all Python source files in a directory tree.
75