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
Don't delete them!!! Until..........
Find a version on your git, svn or copy folder that works.
Delete them and then recover all .pyc.
That's work for me.
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 apyc
from a different version of python (usually later) than your interpreter.If they are your
pyc
files, just delete them and let the interpreter re-compile thepy
files. On UNIX type systems, that could be something as simple as:or:
If they are not yours, you'll have to either get the
py
files for re-compilation, or an interpreter that can run thepyc
files with that particular magic value.One thing that might be causing the intermittent nature. The
pyc
that's causing the problem may only be imported under certain conditions. It's highly unlikely it would import sometimes. You should check 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 is62131
,2.6.1(r261:67517)
is62161
. The list of all magic numbers can be found inPython/import.c
, reproduced here for completeness (current as at the time the answer was posted, it may have changed since then):In my case it was not
.pyc
but old binary.mo
translation files after I renamed my own module, so inside this module folder I had to run(please do backup and try to fix
.pyc
files first)Loading a python3 generated
*.pyc
file with python2 also causes this error.I had a strange case of Bad Magic Number error using a very old (1.5.2) implementation. I generated a .pyo file and that triggered the error. Bizarrely, the problem was solved by changing the name of the module. The offending name was sms.py. If I generated an sms.pyo from that module, Bad Magic Number error was the result. When I changed the name to smst.py, the error went away. I checked back and forth to see if sms.py somehow interfered with any other module with the same name but I could not find any name collision. Even though the source of this problem remained a mistery for me, I recommend trying a module name change.
"Bad magic number" error also happens if you have manually named your file with an extension .pyc