Home > Net >  AttributeError: partially initialized module 'random' has no attribute 'randrange
AttributeError: partially initialized module 'random' has no attribute 'randrange

Time:07-13

import random

print(random.randrange(1, 10))

when i tried this in vs code I am getting an error

CodePudding user response:

You've put this code into a file called random.py haven't you! You need to be careful in Python that your module names aren't the same as other things.

For others, I can replicate this by doing:

cat > random.py << EOF
import random
print(random.randrange(1, 10))
EOF
python random.py 

results in:

Traceback (most recent call last):
  File "/home/smason/random.py", line 1, in <module>
    import random
  File "/home/smason/random.py", line 3, in <module>
    print(random.randrange(1, 10))
AttributeError: partially initialized module 'random' has no attribute 'randrange' (most likely due to a circular import)

CodePudding user response:

Maybe there is a file called random.py outside of your code.

random is a python library, so please do not use it to name your files.

Modify the name of that file or delete it and the problem will be solved.

  • Related