tags:

views:

5390

answers:

3

What's the "Bad magic number" ImportError in python, and how do I fix it?

The only thing I can find online suggests this is caused by compiling a .py -> .pyc file and then trying to use it with the wrong version of python. In my case, however, the file seems to import fine some times but not others, and I'm not sure why.

The information python's providing in the traceback isn't particularly helpful (which is why I was asking here...), but here it is in case it helps:

Traceback (most recent call last):
  File "run.py", line 7, in <module>
    from Normalization import Normalizer
+10  A: 

The magic number comes from UNIX-type systems where the first few bytes of a file held a marker indicating the file type.

Python puts a similar marker into its pyc files when it creates them.

Then the python interpreter makes sure this number is correct when loading it.

Anything that damages this magic number will cause your problem. This includes editing the pyc file or trying to run a pyc from a different version of python (usually later) than your interpreter.

If they're your pyc files, delete them and let the interpreter re-compile the py files.

If they're not yours, you'll have to either get the py files or an interpreter that can run the pyc files with that particular magic value.

One thing that might be causing the intermittent nature. The pyc that's causing the problem files may only be imported under certain conditions. It's highly unlikely it would import sometimes. What's the actual full stack trace when the import fails?

As an aside, the first word of all my 2.5.1(r251:54863) pyc files is 62131, 2.6.1(r261:67517) is 62161. The list of all magic numbers can be found in Python/import.c here (as of 2.7a0), reproduced here for completeness:

1.5:   20121
1.5.1: 20121
1.5.2: 20121
1.6:   50428
2.0:   50823
2.0.1: 50823
2.1:   60202
2.1.1: 60202
2.1.2: 60202
2.2:   60717
2.3a0: 62011
2.3a0: 62021
2.3a0: 62011
2.4a0: 62041
2.4a3: 62051
2.4b1: 62061
2.5a0: 62071
2.5a0: 62081
2.5a0: 62091
2.5a0: 62092
2.5b3: 62101
2.5b3: 62111
2.5c1: 62121
2.5c2: 62131
2.6a0: 62151
2.6a1: 62161
2.7a0: 62171
paxdiablo
Thanks -- this didn't directly help me figure out my problem, but it's nice to know the answer anyways!
Noah
+1  A: 

Deleting all .pyc files will fix "Bad Magic Number" error.

rm -rf `find . -name "*.pyc"`
huvelbaki
It's probably better to use `find . -name "*.pyc" -delete`, as you'll have issues with spaces (and possibly with too long a command line) if you expand all the filenames to pass to `rm`.
Andrew Aylett
A: 

This is much more efficent than above.

find {directory-of-.pyc-files} -name "*.pyc" | xargs rm -rf

where {directory-of-.pyc-files} is the directory that contains the compiled python files.

ozgurv
That is in case you have the py files on hand, if not you will need to downgrade the python install.
Leon