Only Bytecode, No .py Files

Christian Heimes lists at cheimes.de
Tue Jul 26 18:04:07 EDT 2011


Am 26.07.2011 23:20, schrieb Eldon Ziegler:
> That seemed like a good idea but the .py file was complied even though
> it was dated 2001-01-01 00:00:00. Python must be checking something
> beside the date.

The first four bytes of a pyc file contain the magic header. It must
match the magic of the current Python version. The next four bytes
contain the pyc_mtime. It must match the mtime of the corresponding .py
files as returned by fstat().st_mtime. If the magic doesn't match or the
mtime header doesn't match the mtime of the .py file, the pyc is ignored.

Hint: If you run python with the -v option, you can get information why
a pyc file is ignored.

$ touch test.py
$ pycompile test.py
$ python -v -c "import test"
...
# test.pyc matches test.py
import test # precompiled from test.pyc
...
$ touch test.py
$ python -v -c "import test"
...
# test.pyc has bad mtime
import test # from test.py
...

Christian




More information about the Python-list mailing list