This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ImportError: Bad magic number in .pyc file
Type: compile error Stage: resolved
Components: Interpreter Core Versions: Python 2.7
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 2022-04-11 14:57 by admin. 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 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"' > 
$ python2.6 -c 'import tmp'
$ rm
$ 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 make sure that you delete all occurrences of xxxx.pyc in directories preceding the directory containing 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.
Date User Action Args
2022-04-11 14:57:59adminsetgithub: 64993
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