py_compile --- Python ソースファイルのコンパイル¶
ソースコード: Lib/py_compile.py
py_compile モジュールには、ソースファイルからバイトコードファイルを作る関数と、モジュールのソースファイルがスクリプトとして呼び出される時に使用される関数が定義されています。
頻繁に必要となるわけではありませんが、共有ライブラリとしてモジュールをインストールする場合や、特にソースコードのあるディレクトリにバイトコードのキャッシュファイルを書き込む権限がないユーザがいるときには、この関数は役に立ちます。
- 
exception py_compile.PyCompileError¶
- ファイルをコンパイル中にエラーが発生すると送出される例外。 
- 
py_compile.compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP, quiet=0)¶
- Compile a source file to byte-code and write out the byte-code cache file. The source code is loaded from the file named file. The byte-code is written to cfile, which defaults to the PEP 3147/PEP 488 path, ending in - .pyc. For example, if file is- /foo/bar/baz.pycfile will default to- /foo/bar/__pycache__/baz.cpython-32.pycfor Python 3.2. If dfile is specified, it is used as the name of the source file in error messages instead of file. If doraise is true, a- PyCompileErroris raised when an error is encountered while compiling file. If doraise is false (the default), an error string is written to- sys.stderr, but no exception is raised. This function returns the path to byte-compiled file, i.e. whatever cfile value was used.- The doraise and quiet arguments determine how errors are handled while compiling file. If quiet is 0 or 1, and doraise is false, the default behaviour is enabled: an error string is written to - sys.stderr, and the function returns- Noneinstead of a path. If doraise is true, a- PyCompileErroris raised instead. However if quiet is 2, no message is written, and doraise has no effect.- (明示的に指定されたか計算された結果の) cfile のパスがシンボリックリンクだったり通常のファイルでなかったりした場合は、 - FileExistsErrorが送出されます。この動作は、それらのパスにバイトコンパイルされたファイルを書き込む権限がある場合、インポートのときにそのパスを通常のファイルに変えてしまうことを警告するためのものです。これはリネームを使ってバイトコンパイルされたファイルを配置するインポートで、同時にファイル書き込みをしてしまう問題を避けるための副作用です。- optimize は最適化レベルを制御するもので、組み込みの - compile()関数に渡されます。デフォルトは現在のインタープリタの最適化レベルを選ぶ- -1です。- invalidation_mode should be a member of the - PycInvalidationModeenum and controls how the generated bytecode cache is invalidated at runtime. The default is- PycInvalidationMode.CHECKED_HASHif the- SOURCE_DATE_EPOCHenvironment variable is set, otherwise the default is- PycInvalidationMode.TIMESTAMP.- バージョン 3.2 で変更: PEP 3147 に準拠し cfile のデフォルト値を変更しました。以前のデフォルトは file + - 'c'(最適化が有効な場合は- 'o') でした。同時に optimize パラメータも追加されました。- バージョン 3.4 で変更: バイトコードをキャッシュするファイルへの書き込みに - importlibを使うようにコードを変更しました。これにより、例えば権限や write-and-move セマンティクスなどの、ファイルの作成や書き込みの動作が- importlibと一致するようになりました。 cfile がシンボリックリンクであるか通常のファイルでない場合、- FileExistsErrorを送出するという注意書きも追加されました。- バージョン 3.7 で変更: The invalidation_mode parameter was added as specified in PEP 552. If the - SOURCE_DATE_EPOCHenvironment variable is set, invalidation_mode will be forced to- PycInvalidationMode.CHECKED_HASH.- バージョン 3.7.2 で変更: The - SOURCE_DATE_EPOCHenvironment variable no longer overrides the value of the invalidation_mode argument, and determines its default value instead.- バージョン 3.8 で変更: quiet パラメータが追加されました。 
- 
class py_compile.PycInvalidationMode¶
- A enumeration of possible methods the interpreter can use to determine whether a bytecode file is up to date with a source file. The - .pycfile indicates the desired invalidation mode in its header. See キャッシュされたバイトコードの無効化 for more information on how Python invalidates- .pycfiles at runtime.- バージョン 3.7 で追加. - 
TIMESTAMP¶
- The - .pycfile includes the timestamp and size of the source file, which Python will compare against the metadata of the source file at runtime to determine if the- .pycfile needs to be regenerated.
 - 
CHECKED_HASH¶
- The - .pycfile includes a hash of the source file content, which Python will compare against the source at runtime to determine if the- .pycfile needs to be regenerated.
 - 
UNCHECKED_HASH¶
- Like - CHECKED_HASH, the- .pycfile includes a hash of the source file content. However, Python will at runtime assume the- .pycfile is up to date and not validate the- .pycagainst the source file at all.- This option is useful when the - .pycsare kept up to date by some system external to Python like a build system.
 
- 
- 
py_compile.main(args=None)¶
- いくつか複数のソースファイルをコンパイルします。 args で (あるいは args が - Noneであればコマンドラインで) 指定されたファイルをコンパイルし、できたバイトコードを通常の方法で保存します。この関数はソースファイルの存在するディレクトリを検索しません; 指定されたファイルをコンパイルするだけです。 args が- '-'1つだけだった場合、ファイルのリストは標準入力から取られます。- バージョン 3.2 で変更: - '-'のサポートが追加されました。
このモジュールがスクリプトとして実行されると、 main() がコマンドラインで指定されたファイルを全てコンパイルします。一つでもコンパイルできないファイルがあると終了ステータスが 0 でない値になります。
参考
- compileallモジュール
- ディレクトリツリー内の Python ソースファイルを全てコンパイルするライブラリ。