[Python Error] ImportError: bad magic number in ‘random’: b’\x03\xf3\r\n’

What is the bad magic number error in Python?

First, let’s start with the bad magic number in UNIX-type systems. I had to go through this error while importing the random module in my code in Ubuntu. So basically, the magic number comes from UNIX-type systems. The first few bytes of a file are assigned as a marker which indicates the file type.

Python assigns similar magic number as a marker in its .pyc file. If you run the python with a different interpreter or edit your .pyc file than you will encounter the bad magic number error.

Remove the .pyc file from your project.

You can either remove the .pyc file with any of the following UNIX commands:

rm *.pyc

--OR TRY--

find . -name '*.pyc' -delete