What's the Bad Magic Number Error

What's the bad magic number error?

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 are your pyc files, just delete them and let the interpreter re-compile the py files. On UNIX type systems, that could be something as simple as:

rm *.pyc

or:

find . -name '*.pyc' -delete

If they are not yours, you'll have to either get the py files for re-compilation, 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 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 is 62131, 2.6.1(r261:67517) is 62161. The list of all magic numbers can be found in Python/import.c, reproduced here for completeness (current as at the time the answer was posted, it may have changed since then):

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

How to remove Bad magic number error in Pyinstaller

PyInstaller does not bundle or bind a Python interpreter with the EXE file. The EXE file is a compiled binary file which, as it is compiled, does not need an interpreter. A PYC file still requires an interpreter.

You can compile the other files (i.e. source.py) using PyInstaller as well, and then move the compiled EXE file to the same directory as app.exe. In that case you would be able to run your command of import os; os.system("source.exe") from the main program. If your other compiled scripts require modules/libraries not used/included in the app.exe compilation, then when moving the compiled source.exe, you will need to move the bundled libraries to the app.exe directory as well.

Tflearn bad magic number error?

Deleting .pyc files, worked.

find . -name '*.pyc' -delete in the tflearn install directory as
suggested in stackoverflow.com/a/514395/7738328`

Bad magic number in importing sklearn

I find my own workaround for this problem. Mr. J's answer and the one from this post didn't solve my problem. So for anyone that experiencing the same thing: My workaround is to Uninstall Anaconda all over again, and install Anaconda Python 2.7 version. From there, create a new anaconda environment in your Python's version of choice. Tadaa fixed.

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.



Related Topics



Leave a reply



Submit