Skip to content Skip to sidebar Skip to footer

Python "import Random" Error

As you may know from my previous posts, I'm learning Python. And this time I have a small error which I think is with this build of Python itself. When using the following: impor

Solution 1:

By naming your script random.py, you've created a naming conflict with the random standard library module.

When you try to run your script, the directory containing the script will be added to the start of the module import path. So when your script does import random, you're effectively running a second copy of the script as the random module.

When the random module runs import random, it means that random.random will also be a reference to your module. So when you attempt to call the random.random() standard library function, you're actually attempting to call the module object resulting in the error you got.

If you rename your script to something else, the problem should go away.

Solution 2:

I am using pycharm and I had to take the additional step to import the methods from random. In my case:

import random
 from random import choice

Solution 3:

Even I faced the same problem. I have renamed my python file from random.py to shuffle.py. This didn't work out. Then I changed the version then it worked. This may help a bit. Python version : 3.6.7 replace import random; to import random2;

Solution 4:

The simple answer: Change your filename from "random.py" to something else as it is conflicting with random library.

Post a Comment for "Python "import Random" Error"