Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1 | \section{\module{py_compile} --- |
| 2 | Compile Python source files} |
| 3 | |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 4 | % Documentation based on module docstrings, by Fred L. Drake, Jr. |
| 5 | % <fdrake@acm.org> |
| 6 | |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 7 | \declaremodule[pycompile]{standard}{py_compile} |
| 8 | |
| 9 | \modulesynopsis{Compile Python source files to byte-code files.} |
| 10 | |
| 11 | |
| 12 | \indexii{file}{byte-code} |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 13 | The \module{py_compile} module provides a function to generate a |
| 14 | byte-code file from a source file, and another function used when the |
| 15 | module source file is invoked as a script. |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 16 | |
| 17 | Though not often needed, this function can be useful when installing |
| 18 | modules for shared use, especially if some of the users may not have |
| 19 | permission to write the byte-code cache files in the directory |
| 20 | containing the source code. |
| 21 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame^] | 22 | \begin{excdesc}{PyCompileError} |
| 23 | Exception raised when an error occurs while attempting to compile the file. |
| 24 | \end{excdesc} |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 25 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame^] | 26 | \begin{funcdesc}{compile}{file\optional{, cfile\optional{, dfile\optional{, doraise}}}} |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 27 | 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}. |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame^] | 33 | If \var{doraise} = True, a PyCompileError is raised when an error is |
| 34 | encountered while compiling \var{file}. If \var{doraise} = False (the default), |
| 35 | an error string is written to sys.stderr, but no exception is raised. |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 38 | \begin{funcdesc}{main}{\optional{args}} |
| 39 | Compile several source files. The files named in \var{args} (or on |
| 40 | the command line, if \var{args} is not specified) are compiled and |
| 41 | the resulting bytecode is cached in the normal manner. This |
| 42 | function does not search a directory structure to locate source |
| 43 | files; it only compiles files named explicitly. |
| 44 | \end{funcdesc} |
| 45 | |
| 46 | When this module is run as a script, the \function{main()} is used to |
| 47 | compile all the files named on the command line. |
| 48 | |
Fred Drake | 4756bb3 | 1998-08-11 15:46:25 +0000 | [diff] [blame] | 49 | \begin{seealso} |
| 50 | \seemodule{compileall}{Utilities to compile all Python source files |
| 51 | in a directory tree.} |
| 52 | \end{seealso} |