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


I’m getting this error when I attempt to write any code using random numbers.

I don’t understand why this is happening. Here is an example:

workspace $ python3
Python 3.4.3 (default, Sep 14 2016, 12:36:27)
[GCC 4.8.4] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import random
Traceback (most recent call last):
File “”, line 1, in
ImportError: bad magic number in ‘random’: b’\x03\xf3\r\n’


A bit of Googling leads me to asking if you were using a different version of Python for awhile and then upgraded to 3.4.2 recently? If so, do you have a version of the file where you import datetime that was run with the old version? If so, you should have a file with the extension .pyc. Delete any .pyc files you have and try again.