12

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user1641423
  • 143
  • 1
  • 2
  • 8
  • 1
    Check out http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error?rq=1 – Matthew Adams Sep 02 '12 at 06:49
  • i dont have .py file to recompile it. I have only .pyc but dont know why python cant import it. Versions are the same – user1641423 Sep 02 '12 at 10:46
  • I found that this file is .pyo (first time i found it with no extension). Is any differents .pyo and .pyc files? Or they are the same? – user1641423 Sep 02 '12 at 11:20
  • 1
    `.pyc` and `.pyo` are pretty similar. They are both versions of python code that have been "byte-compiled" so that they load faster when they are imported as modules into other python programs. `.pyo` is basically a slightly more optimized version of `.pyc`. (For more details see [the doc](http://docs.python.org/tutorial/modules.html#compiled-python-files).) – Matthew Adams Sep 02 '12 at 13:47

3 Answers3

8

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.

Community
  • 1
  • 1
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Interesting thing. The .pyc file i need to import has magic: C0 00 00 00. It running from .Exe packed with pyInstaller. Python version 2.6.6 (DLL). Maybe PyInstaller change magic and possible structure of .pyc? – user1641423 Sep 02 '12 at 21:28
  • 1
    Are those hexadecimal bytes and if so, how did you obtain them? That does not look like a valid Python magic number, which might be why Python is refusing to import the file. – user4815162342 Sep 03 '12 at 05:01
  • 1
    This is first 4 bytes from .pyc file. I know it looks weird. Now i need to watch pyInstaler source maybe it makes correction of magic and over thing on fly while program get started – user1641423 Sep 03 '12 at 09:17
3

I solved this by running

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

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

Ash Berlin-Taylor
  • 3,879
  • 29
  • 34
Dave McNulla
  • 2,006
  • 16
  • 23
0

If your running python2 and python3 and removing old *.pyc files is too messy you can add the following to try to patch this python3 bug by adjusting the code loader suffix for bytecode files.

import sys
if(sys.version_info.major>=3): # switch byte files end extension on 3
    import importlib.machinery
    altsuffix = ['.pyc3'] # or some other ending that doesn't create conflicts
    importlib.machinery.BYTECODE_SUFFIXES = altsuffix
user6830669
  • 161
  • 4