Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exception: AttributeError: module 'random' has no attribute

  • A common mistake. Using the wrong filename.

This works fine:

print("Hello World")

This gives an error

import random
print(random.random())
Traceback (most recent call last):
  File "rnd.py", line 2, in <module>
    print(random.random())
AttributeError: module 'random' has no attribute 'random'

Make sure the names of your files are not the same as the names of any of the python packages.