What's the bad magic number error?

后端 未结 15 2142
心在旅途
心在旅途 2020-11-22 16:56

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

相关标签:
15条回答
  • 2020-11-22 17:32

    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

    0 讨论(0)
  • 2020-11-22 17:33

    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)

    0 讨论(0)
  • 2020-11-22 17:34

    "Bad magic number" error also happens if you have manually named your file with an extension .pyc

    0 讨论(0)
  • 2020-11-22 17:35

    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

    • 1.5 => 20121 => 4E99 so files would have first byte as 99 and second as 4e
    • 1.6 => 50428 => C4FC so files would have first byte as fc and second as c4
    0 讨论(0)
  • 2020-11-22 17:36

    Loading a python3 generated *.pyc file with python2 also causes this error.

    0 讨论(0)
  • 2020-11-22 17:42

    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

    0 讨论(0)
提交回复
热议问题