What's the bad magic number error?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A "bad magic number" error means a program read the beginning of some file or binary data and found a signature that did not match what it expected. In computing, a magic number is not mystical. It is just a recognizable header value used to identify a file format, bytecode version, protocol message, or binary structure.
What a Magic Number Is
Many file formats begin with a fixed signature. Examples include:
- PNG starts with a specific binary header
- JPEG starts with a known marker pattern
- Python bytecode files contain a version-specific header
Programs use those first bytes to verify that the input is really the type of data they think it is. If the signature is wrong, they often stop immediately instead of trying to parse nonsense.
That failure is what people usually mean by a bad magic number.
A General Example
Suppose a parser expects a PNG file and checks the first bytes:
If the file was corrupted, mislabeled, or not actually a PNG at all, the parser rejects it immediately.
A Very Common Python Case
In Python, a bad magic number error often appears when a .pyc file was created by a different Python version than the interpreter currently trying to load it.
That is why the message is common after:
- switching Python versions
- copying cached bytecode between environments
- restoring a project directory from an old build artifact
The fix is usually to delete stale bytecode caches and let Python regenerate them:
On the next run, Python rebuilds compatible bytecode for the current interpreter.
Other Situations That Trigger It
The same pattern appears outside Python too. A bad magic number can mean:
- the file is truncated or corrupted
- the file extension is misleading
- the wrong parser or tool is being used
- data was transferred in text mode instead of binary mode
- one binary format was mistaken for another
The unifying idea is always the same: the reader expects one signature and receives another.
Why Tools Check Headers Early
Checking the magic number early is a defensive programming technique. It prevents deeper parsing logic from interpreting the wrong bytes as valid structures.
Without that validation, the program might continue with corrupted assumptions and fail later in a harder-to-debug way. So although the error is annoying, it is actually a helpful integrity check.
Practical Debugging Steps
When you see a bad magic number error, ask these questions in order:
- is this really the file type or bytecode version the tool expects
- was the file generated by the correct toolchain or interpreter
- could the file be corrupted or partially copied
- am I reading binary data with the right mode and parser
Those questions usually narrow the cause quickly.
Common Pitfalls
The most common mistake is trusting the file extension. Renaming a file to .png or .pyc does not make its contents valid for that format.
Another issue is mixing build artifacts or caches between incompatible runtime versions, especially in Python and other compiled-bytecode workflows.
Developers also assume the error means the parser is buggy. More often, it means the input is wrong for that parser.
Summary
- A bad magic number error means a file or binary header did not match the expected signature.
- Magic numbers are format-identifying bytes placed at the start of data.
- The error often points to corruption, wrong file type, or version mismatch.
- In Python, stale
.pycfiles from another interpreter version are a common cause. - The fix is usually to verify the real file type, regenerate stale artifacts, or use the correct reader for the data.

