How to Solve Python AttributeError: ‘int’ object has no attribute ‘randint’

by | Programming, Python, Tips

This error occurs if you try to call randint() method on an integer. You can solve this error by not naming an object random, which will override the reserved name for the built-in module random.

For example,

import random

my_int = 10

random_int = random.randint(1,10)

This tutorial will go through the error in detail and how to solve it.


AttributeError: ‘int’ object has no attribute ‘randint’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘int’ object has no attribute ‘randint’” tells us that the string object we handle does not have the attribute randint().

randint() is a method of the random module that returns an integer between a specified range.

We can check if an attribute exists for an object using the dir() function. For example,

my_int = 47

print(type(my_int))

print('randint' in dir(my_int))
<class 'int'>
False

We can see that randint() is not present in the list of attributes for the int object.

Example

Let’s look at an example of reproducing the error.

# Import random module

import random

# Define integer

random = 2900

# Attempt to print a random integer between 15 and 20

print(random.randint(15, 20))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 5>()
      1 import random
      3 random = 2900
----> 5 print(random.randint(15, 20))

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

The error occurs because we assigned an integer value to the variable name random, which overrides the reserved name for the random module. We should not use the names of modules to define variables.

We can check the type of an object using the type() function.

import random

print(type(random))

random = 2900

print(type(random))

print(random.randint(15, 20))
<class 'module'>
<class 'int'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [6], in <cell line: 9>()
      5 random = 2900
      7 print(type(random))
----> 9 print(random.randint(15, 20))

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

We can see that when we import random and print the type, we get the type module then, when we assign an integer to the name random, the type of the object is int. Therefore, when we try to call the randint() method from the random module, we are calling randint() on the integer.

Solution #1

We can solve the error by naming the variable something other than the reserved name random. Let’s look at the revised code:

import random

print(type(random))

my_int = 2900

print(type(random))

print(random.randint(15, 20))

Let’s run the code to see the result:

<class 'module'>
<class 'module'>
19

We did not override the name random for the module and were able to call the random.randint() method.

Solution #2

We can also solve the error by importing the randint method from the random module using the from keyword.

Using this approach, we do not need to change the variable name from random. However, it is still good practice to not give variables reserved names.

Let’s look at the revised code:

from random import randint

random = 2900

print(type(random))

print(randint(15, 20))

Let’s run the code to see the result:

<class 'int'>
18

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨