fuckingpython.lol

Python errors › AttributeError

AttributeError: module has no attribute

Updated

The Python error "module 'random' has no attribute 'randint'" usually means your own file is named like the module. Python 3.13 says so outright. The fix.

What you see

random.py
import random

print(random.randint(1, 6))
Python 3.15 prints
Traceback (most recent call last):
  File "/home/me/project/random.py", line 1, in <module>
    import random
  File "/home/me/project/random.py", line 3, in <module>
    print(random.randint(1, 6))
          ^^^^^^^^^^^^^^
AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/project/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)

What it means

The folder of the script you run comes first on Python's import path. A file called random.py there wins over the standard library's random, so import random imports your own file, which has no randint.

Python 3.13 and later spot this and say it in the message: consider renaming the file, because it has the same name as the standard library module. Older versions call it a "partially initialized module" and blame a circular import, which is technically true: your file is importing itself.

Why it happens

  • A script named after a standard library module: random.py, math.py, email.py, socket.py, json.py, string.py, turtle.py, calendar.py.
  • A script named after a package you use: requests.py, numpy.py, pandas.py, openai.py. Python 3.13 catches this too and suggests renaming.
  • A folder with the package's name in your project, which Python can import as an empty namespace package.
  • The attribute really is gone from the version you have installed. Libraries rename and remove things: NumPy 1.24 removed np.float, for example. Check the library's changelog if the file name is not the cause.

How to fix it

Rename your file to something no module uses, and run it again:

mv random.py dice.py
python3 dice.py

To see which file Python actually imported, print its path:

import random
print(random.__file__)

If the path is inside your project rather than inside Python's library folder, that file is the cause.

How each Python version words it

PythonPrints
3.9–3.12AttributeError: partially initialized module 'random' has no attribute 'randint' (most likely due to a circular import)
3.13–3.15AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/project/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.

3.9 to 3.12 report "partially initialized module 'random' has no attribute 'randint' (most likely due to a circular import)". 3.13 and later name the file and suggest renaming it, with a slightly different hint for third-party packages.

Questions

I renamed the file and it still fails

Look for another file or folder with the module's name in the same directory, such as a random/ folder. A leftover __pycache__/random.cpython-*.pyc is not the cause: Python ignores a cached file once its source is gone.

Why is the script's own folder searched first?

So that a script can import the modules next to it without installing them. The price is that its names can shadow the standard library.

Sources

Read on October 25, 2026.