Bad magic number while trying to import .pyc module

I have some problems while trying to import some module (compiled .pyc) in my program. I know that it compiled in Python 2.6.6 (r266:84297), I have installed the same version, but had an error "bad magic number" while trying to import it :(

Does anybody know what I did wrong? Or maybe it's possible to change magic number in .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.


I solved this by running

find . | grep .pyc$ | xargs rm

which deleted all the pyc files in my directory. After that it was OK.

链接地址: http://www.djcxy.com/p/63322.html

上一篇: 装饰者模式的实现

下一篇: 尝试导入.pyc模块时出现错误的幻数