Bad magic number while trying to import .pyc module

As the answer linked by Matthew explains, your problem is almost certainly due to different versions of Python being used for compiling and loading the module. You can determine the magic number like this:

with open('pyuca.pyc', 'rb') as f:
    print struct.unpack('<H', f.read(2))

You can determine your Python version by printing sys.version (it is also echoed on interactive startup). If you are using Python 2.6.6, the magic number should be 62161. If it is different, you will need to switch to a different Python to be able to import the module.

The exact same applies to .pyo files.


I solved this by running

find . -name '*.pyc' -exec rm {} +

which deleted all the pyc files in my directory. After that it was OK.

Tags:

Python