blob: b5b90107ecaa8dee71e11730e5a33c760e51ae7b [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
Georg Brandl8334fd92010-12-04 10:26:46 +000025.. function:: compile(file, cfile=None, dfile=None, doraise=False, optimize=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000026
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
Barry Warsaw28a691b2010-04-17 00:19:56 +000029 *cfile*, which defaults to the :PEP:`3147` path, ending in ``.pyc``
30 (``'.pyo`` if optimization is enabled in the current interpreter). For
31 example, if *file* is ``/foo/bar/baz.py`` *cfile* will default to
32 ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. If *dfile* is specified, it is used as the
Georg Brandl116aa622007-08-15 14:28:22 +000033 name of the source file in error messages instead of *file*. If *doraise* is
34 true, a :exc:`PyCompileError` is raised when an error is encountered while
35 compiling *file*. If *doraise* is false (the default), an error string is
Barry Warsaw28a691b2010-04-17 00:19:56 +000036 written to ``sys.stderr``, but no exception is raised. This function
37 returns the path to byte-compiled file, i.e. whatever *cfile* value was
38 used.
Georg Brandl116aa622007-08-15 14:28:22 +000039
Georg Brandl8334fd92010-12-04 10:26:46 +000040 *optimize* controls the optimization level and is passed to the built-in
41 :func:`compile` function. The default of ``-1`` selects the optimization
42 level of the current interpreter.
43
44 .. versionchanged:: 3.2
45 Added the *optimize* parameter.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandl18244152009-09-02 20:34:52 +000048.. function:: main(args=None)
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 Compile several source files. The files named in *args* (or on the command
Georg Brandl18244152009-09-02 20:34:52 +000051 line, if *args* is ``None``) are compiled and the resulting bytecode is
Georg Brandl116aa622007-08-15 14:28:22 +000052 cached in the normal manner. This function does not search a directory
53 structure to locate source files; it only compiles files named explicitly.
54
55When this module is run as a script, the :func:`main` is used to compile all the
Christian Heimesdd15f6c2008-03-16 00:07:10 +000056files named on the command line. The exit status is nonzero if one of the files
57could not be compiled.
58
Georg Brandl116aa622007-08-15 14:28:22 +000059
60.. seealso::
61
62 Module :mod:`compileall`
63 Utilities to compile all Python source files in a directory tree.
64