Random Module AttributeError , 'random' has no attribute 'randint'

Question:

import random
a = random.randint(5, 10)
print(a)

This code won’t execute in VSCode, but works just fine in powershell.
Here is the error I have been getting in VSCode

enter image description here

Edit: The file was named random.py, and thus caused the error.

Asked By: Illusioner_

||

Answers:

Rename your file from random.py to something else. Python is getting confused because the file name shadows the random module.

BTW, this is what the circular import warning is about. You are importing random, which is the file name. So, it imports the file you wrote. Whenever it imports the file, import random is executed, which imports the file you wrote…

Answered By: SaltyShrub
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.