2

I have written out the following code to try and generate random numbers-

import random
def main(): 
   a=random.randint(1,100)
   b=random.randint(1,100) 
   print (a)
   print (b)

main() 

After running the program, I got this message:

File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py", line 41, in module

from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil

ImportError: bad magic number in 'math': b'\x03\xf3\r\n

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
isaac
  • 23
  • 1
  • 5
  • Your Python installation may be corrupt. –  Sep 11 '14 at 00:05
  • Have you explicitly added your Python 3.4 stdlib to your `PYTHONPATH`? And then tried to run some different Python (maybe a different 3.4 that you installed via Homebrew, or maybe Apple's pre-installed 2.7)? – abarnert Sep 11 '14 at 00:07
  • Strike that and reverse it. `b'\x03\xf3\r\n'` is the magic number from CPython 2.7, so you've apparently added some Python 2.7 stdlib to your `PYTHONPATH` and run 3.4… – abarnert Sep 11 '14 at 00:09
  • I got the bad magic number on 'argparse' and it turns out that in one of my PYTHONPATH folders there is a 'argparse.pyc'. I don't remember when and why I put it there but apparently that's the problem. – Jason Sep 21 '17 at 14:58

1 Answers1

5
ImportError: bad magic number in 'math': b'\x03\xf3\r\n

The magic number b'\x03\xf3\r\n' means Python 2.7. I'm not sure where to find that online, but since you're on a Mac, you can just run Apple's pre-installed Python2.7 and see:

$ /usr/bin/python2.7
>>> import imp
>>> imp.get_magic()
'\x03\xf3\r\n'

So, somehow you have a 2.7 math.pyc on your sys.path, and of course 3.4 can't load it.


Also note that the standard CPython math module, both in 2.x and 3.x, is a pure C extension module—that is, it's a .so file, not a .py file, so there will be no .pyc for that module. You must have either written or downloaded some module named math.py and used it—in Python 2.7—at some point.

Unless you use a non-CPython implementation, in which case there could conceivably be a math.py in the stdlib. If you're using, say, PyPy, I suspect you'd know that you're using it, so if you have no idea what I'm talking about, retroactively skip this paragraph.


Most likely it's just in the current directory or its __pycache__. To find out, do this:

$ find . -name 'math*.pyc'

If anything turns up, that's the problem, and you have to delete it. But first, you may want to make sure that you still have the math.py file it came from, or don't need it. (Or maybe just move it somewhere out of the way instead of deleting it, if you're not sure.)


However, it's also possible that you've installed it somewhere that shouldn't be on your sys.path but is—or that you installed it with 2.7 to somewhere that should be on your 3.4 sys.path but not your 2.7 sys.path, but is.

The easiest way to find out where a module is when you can't successfully import it and look at it, in 3.4+, is:

>>> import importlib
>>> importlib.util.find_spec('math')
ModuleSpec(name='math', loader=<_frozen_importlib.ExtensionFileLoader object at 0x102e065c0>, origin='/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload/math.so')

You're going to find some file named math.pyc (or math-SOMETHING.pyc) somewhere. What you then need to figure out is whether that "somewhere" shouldn't be on your path, or whether that file shouldn't be in that somewhere. There's a good chance this will be obvious, because the path to the file will have either 2.7 or 3.4 in it.

If you want to know what your path is, just do this:

>>> import sys
>>> sys.path

That should show you a list of paths including '.', the 3.4 stdlib, the 3.4 system and user site-packages, any eggs that are installed with .pth files, etc. It should not include anything 2.7 or unversioned.

abarnert
  • 354,177
  • 51
  • 601
  • 671