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.