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
I just faced the same issue with Fedora26 where many tools such as dnf were broken due to bad magic number for six. For an unknown reason i've got a file /usr/bin/six.pyc, with the unexpected magic number. Deleting this file fix the problem
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
find . -name \*.po -execdir sh -c 'msgfmt "$0" -o `basename $0 .po`.mo' '{}' \;
(please do backup and try to fix .pyc
files first)
"Bad magic number" error also happens if you have manually named your file with an extension .pyc
Take the pyc file to a windows machine. Use any Hex editor to open this pyc file. I used freeware 'HexEdit'. Now read hex value of first two bytes. In my case, these were 03 f3.
Open calc and convert its display mode to Programmer (Scientific in XP) to see Hex and Decimal conversion. Select "Hex" from Radio button. Enter values as second byte first and then the first byte i.e f303 Now click on "Dec" (Decimal) radio button. The value displayed is one which is correspond to the magic number aka version of python.
So, considering the table provided in earlier reply
Loading a python3 generated *.pyc
file with python2 also causes this error.
So i had the same error :importError bad magic number. This was on windows 10
This error was because i installed mysql-connector
So i had to; pip uninstall mysql-comnector pip uninstall mysql-connector-python
pip install mysql-connector-python