Poor number of mages trying to import .pyc module

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

Does anyone know what I did wrong? Or is it possible to change the magic number in the .pyc module?

+8
source share
2 answers

As the answer related to Matthew explains, your problem is almost certainly due to the fact that different versions of Python are used to compile and load the module. You can define the magic number as follows:

with open('pyuca.pyc', 'rb') as f: print struct.unpack('<H', f.read(2)) 

You can determine your version of Python by typing sys.version (this also repeats on interactive start). If you are using Python 2.6.6, the magic number should be 62161. If it is different, you will need to switch to another Python in order to be able to import the module.

The same goes for .pyo files.

+4
source

I solved this by running

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

which deleted all the pyc files in my directory. After that, everything was in order.

0
source

All Articles