classification
Title: ImportError: Bad magic number in .pyc file
Type: compile error Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: navalkgupta, ned.deily, peter.otten, vstinner
Priority: normal Keywords:

Created on 2014-02-27 12:13 by navalkgupta, last changed 2014-02-27 20:44 by ned.deily. This issue is now closed.

Messages (4)
msg212351 - (view) Author: Naval Gupta (navalkgupta) Date: 2014-02-27 12:13
Getting below compilation error:

ImportError: Bad magic number in xxxx.pyc

Ran a python abc.py module which is trying to import xxxx.pyc during compilation

Python Interpreter used to run: Python 2.7
Python pre-compiled file xxxx.pyc is compiled using Python 2.6
msg212352 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-02-27 13:02
Can you please attach your script and give the command you are typing to reproduce the issue?
msg212353 - (view) Author: Peter Otten (peter.otten) * Date: 2014-02-27 13:22
This is expected. You cannot run/import 2.6 .pyc files in python 2.7 because the file format has changed.

$ echo 'print "hello"' > tmp.py 
$ python2.6 -c 'import tmp'
hello
$ rm tmp.py
$ python2.7 -c 'import tmp'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: Bad magic number in tmp.pyc

Naval, if you do have the source xxxx.py make sure that you delete all occurrences of xxxx.pyc in directories preceding the directory containing xxxx.py in sys.path. Sometimes this error is the result of moving a .py file into another directory and forgetting to delete the leftover .pyc.
msg212384 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-02-27 20:44
As Peter notes, .pyc and .pyo files are in general not compatible across Python versions.  PEP 3147 was implemented in Python 3.2 to help avoid the problem you are seeing by storing .pyc/.pyo files with version-specific filenames.  For Python 2, you need to be careful to recompile (see "compileall") or delete .pyc/.pyo files when changing interpreter versions. 

http://docs.python.org/3/whatsnew/3.2.html#pep-3147-pyc-repository-directories
http://docs.python.org/2/library/compileall.html
History
Date User Action Args
2014-02-27 20:44:49ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg212384

resolution: not a bug
stage: resolved
2014-02-27 13:22:03peter.ottensetnosy: + peter.otten
messages: + msg212353
2014-02-27 13:02:43vstinnersetnosy: + vstinner
messages: + msg212352
2014-02-27 12:13:07navalkguptacreate