Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad magic number while trying to import .pyc module

Tags:

python

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?

like image 730
user1641423 Avatar asked Sep 02 '12 06:09

user1641423


2 Answers

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.

like image 197
user4815162342 Avatar answered Oct 31 '22 22:10

user4815162342


I solved this by running

find . -name '*.pyc' -exec rm {} +

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

like image 21
Dave McNulla Avatar answered Oct 31 '22 22:10

Dave McNulla