Understanding the "Bad Magic Number" ImportError
The "Bad magic number" ImportError occurs in Python when an imported pyc file contains an incorrect magic number, which is a marker indicating the file type in UNIX-like systems.
This error can arise due to several reasons:
To resolve this issue, follow these steps:
Delete Pyc Files: Delete all .pyc files associated with the imported module. This forces the interpreter to recompile the .py files upon import. You can use the following commands in a UNIX terminal:
rm *.pyc
find . -name '*.pyc' -delete
Magic Number Values: Refer to the following table for the magic numbers associated with various Python versions:
Python Version | Magic Number |
---|---|
1.5, 1.5.1, 1.5.2 | 20121 |
1.6 | 50428 |
2.0, 2.0.1 | 50823 |
2.1, 2.1.1, 2.1.2 | 60202 |
2.2 | 60717 |
2.3a0 (various builds) | 62011, 62021, 62041 |
2.4a0 | 62051 |
2.4a3 | 62061 |
2.5a0 (various builds) | 62071, 62081, 62091, 62092 |
2.5b3 (various builds) | 62101, 62111 |
2.5c1 | 62121 |
2.5c2 | 62131 |
2.6a0 | 62151 |
2.6a1 | 62161 |
2.7a0 | 62171 |
Understanding the bad magic number error and its causes will assist you in effectively resolving this issue when encountered.
The above is the detailed content of Why Do I Get a \'Bad Magic Number\' ImportError in Python, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!