PEP 3147 - new .pyc format

Ben Finney ben+python at
Sun Jan 31 03:39:49 CET 2010

Steven D'Aprano <steve at> writes:

> Unfortunately the magic number doesn't seem to be documented anywhere
> I can find other than in the source code (import.c). The PEP gives
> some examples:
>     f2b30a0d.pyc # Python 2.5
>     f2d10a0d.pyc # Python 2.6
>     f2d10a0d.pyo # Python 2.6 -O
>     f2d20a0d.pyc # Python 2.6 -U
>     0c4f0a0d.pyc # Python 3.1
> but how can one map magic numbers to versions, short of reading
> import.c? 

Mapping magic numbers to versions is infeasible and will be incomplete:
Any mapping that exists in (say) Python 3.1 can't know in advance what
the magic number will be for Python 4.5.

The more important mapping is from version to magic number.

> I propose that sys grow an object sys.magic which is the hexlified magic 
> number.

The ‘imp’ module already has this::

    >>> import sys
    >>> import imp
    >>> sys.version
    '2.5.4 (r254:67916, Jan 24 2010, 16:09:54) \n[GCC 4.4.2]'
    >>> imp.get_magic().encode('hex')

Unfortunately, I think the examples in the PEP have mangled the magic
number into little-endian byte ordering.

 \         “I got up the other day, and everything in my apartment has |
  `\   been stolen and replaced with an exact replica.” —Steven Wright |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list