1

I have a compiled Python script called merlin.pyc which when I run it via a bash script in Linux returns:

RuntimeError: Bad magic number in .pyc file

I understand that this can be caused by using different versions of Python to compile the .py into .pyc and to the version that you run the .pyc with. I however complied merlin.py into merlin.pyc on the same Python build on the same platform a the one I am trying to run merlin.pyc with. I complied and am trything to run the python code using Python 2.7.13.

Are there any other possible causes for a bad magic number?

Edit

Please see below for output of dd if=merlin.pyc bs=8 count=1 | hexdump -C:

enter image description here

Community
  • 1
  • 1
Stacey
  • 4,825
  • 17
  • 58
  • 99
  • The bottom line is that the file isn't valid; how it *became* invalid is almost academic. That said, a place to start would be to show us the header it actually came with so we can figure out what its current magic *is*. – Charles Duffy Feb 22 '19 at 21:16
  • Possible duplicate of [What's the bad magic number error?](https://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error) – Valentino Feb 22 '19 at 21:16
  • Edit output of `dd if=yourfile.pyc bs=8 count=1 | hexdump -C` (or similar) into the question, and we can see what the actual magic is, and thus which version of Python the module *thinks* it's for. That'll also allow the assertion (that the same build was used) to be validated. – Charles Duffy Feb 22 '19 at 21:17
  • Thanks #Charles Duffy please see question exit for output – Stacey Feb 22 '19 at 21:48
  • okay, so it *does* start with `03f3`, so that really is correct for Python 2.7... can you similarly (albeit ideally as copied/pasted text, not a screenshot image) demonstrate that it's a 2.7 interpreter throwing the error? – Charles Duffy Feb 22 '19 at 22:02
  • Hmm. Thinking about it -- could a different module that `merlin.pyc` imports be the one with the bad magic? (That one itself really does look fine) -- one way to test would be to trace the interpreter with `strace` or similar tools and watch which files it loads/reads. – Charles Duffy Feb 22 '19 at 22:05
  • Thanks, Not used strace before, how would I run strace? – Stacey Feb 22 '19 at 22:15

0 Answers0