blob: 85f0aaa8bbcb46ab483f7825abe75f7d2a9b86de [file] [log] [blame]
Fred Drake38e5d272000-04-03 20:13:55 +00001\section{\module{py_compile} ---
2 Compile Python source files}
3
Fred Drake4756bb31998-08-11 15:46:25 +00004% Documentation based on module docstrings, by Fred L. Drake, Jr.
5% <fdrake@acm.org>
6
Fred Drake4756bb31998-08-11 15:46:25 +00007\declaremodule[pycompile]{standard}{py_compile}
8
9\modulesynopsis{Compile Python source files to byte-code files.}
10
11
12\indexii{file}{byte-code}
Fred Drake61cf4402002-08-21 20:56:21 +000013The \module{py_compile} module provides a function to generate a
14byte-code file from a source file, and another function used when the
15module source file is invoked as a script.
Fred Drake4756bb31998-08-11 15:46:25 +000016
17Though not often needed, this function can be useful when installing
18modules for shared use, especially if some of the users may not have
19permission to write the byte-code cache files in the directory
20containing the source code.
21
Martin v. Löwis0c6774d2003-01-15 11:51:06 +000022\begin{excdesc}{PyCompileError}
23Exception raised when an error occurs while attempting to compile the file.
24\end{excdesc}
Fred Drake4756bb31998-08-11 15:46:25 +000025
Martin v. Löwis0c6774d2003-01-15 11:51:06 +000026\begin{funcdesc}{compile}{file\optional{, cfile\optional{, dfile\optional{, doraise}}}}
Fred Drake4756bb31998-08-11 15:46:25 +000027 Compile a source file to byte-code and write out the byte-code cache
28 file. The source code is loaded from the file name \var{file}. The
29 byte-code is written to \var{cfile}, which defaults to \var{file}
30 \code{+} \code{'c'} (\code{'o'} if optimization is enabled in the
31 current interpreter). If \var{dfile} is specified, it is used as
32 the name of the source file in error messages instead of \var{file}.
Georg Brandldb815ab2006-03-17 16:26:31 +000033 If \var{doraise} is true, a \exception{PyCompileError} is raised when
34 an error is encountered while compiling \var{file}. If \var{doraise}
35 is false (the default), an error string is written to \code{sys.stderr},
36 but no exception is raised.
Fred Drake4756bb31998-08-11 15:46:25 +000037\end{funcdesc}
38
Fred Drake61cf4402002-08-21 20:56:21 +000039\begin{funcdesc}{main}{\optional{args}}
40 Compile several source files. The files named in \var{args} (or on
41 the command line, if \var{args} is not specified) are compiled and
42 the resulting bytecode is cached in the normal manner. This
43 function does not search a directory structure to locate source
44 files; it only compiles files named explicitly.
45\end{funcdesc}
46
47When this module is run as a script, the \function{main()} is used to
48compile all the files named on the command line.
49
Fred Drake4756bb31998-08-11 15:46:25 +000050\begin{seealso}
51 \seemodule{compileall}{Utilities to compile all Python source files
52 in a directory tree.}
53\end{seealso}