AttributeError: 'int' object has no attribute 'randint'


Borislav Hadzhiev

Last updated: Apr 20, 2022


Photo from Unsplash

AttributeError: 'int' object has no attribute 'randint' #

The Python "AttributeError: 'int' object has no attribute 'randint'" occurs when we assign the random module to an integer and try to call the randint() method. To solve the error, make sure to import random and call the method as random.randint(...).

attributeerror int object has no attribute randint

Here is an example of how the error occurs.
import random random = 100 # ⛔️ AttributeError: 'int' object has no attribute 'randint' print(random.randint(1, 10))

We imported the random module and reassigned it to an integer before calling the randint() method which caused the error.

If you print() the value you are calling randint() on, it will be an integer.

To solve the error, you need to track down where exactly you are setting the random module to an integer in your code and remove the assignment.

To solve the error in the example, we would have to remove the reassignment.
import random print(random.randint(1, 10)) # 👉️ 8

Alternatively, you can directly import the randint function from the random module.
from random import randint print(randint(1, 10)) # 👉️ 10

The random.randint function takes 2 numbers - a and b as parameters and returns a random integer in the range.

Note that the range is inclusive - meaning both a and b can be returned.

Calling the method on an integer causes the error.

To solve the error, you have to track down where the random module gets assigned an integer and remove the assignment.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.