How to Solve Python NameError: name ‘raw_input’ is not defined

by | Programming, Python, Tips

The built-in raw_input() function in Python 2 does not exist in Python 3. In Python 3, we can use the function input() to collect input from the user of a program If you try to use raw_input() in a Python 3 program, you will raise the NameError: name ‘raw_input’ is not defined.

To solve this error, use the input() function instead of raw_input() on Python 3.

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


NameError: name ‘raw_input’ is not defined

The NameError exception occurs when the object we want to call is not initialized in the current scope of the Python program. The raw_input() function was replaced by input(). The syntax of input() is as follows:

input(prompt)

Parameters

  • prompt: Optional. A String representing a default message before the input.

Difference Between input() and raw_input() in Python

The input() function exists in both versions of Python 2 and 3. In Python 3, the input() function explicitly converts the input provided to the type string. In Python 2, the input() function does not modify the type of the input value. Let’s look at examples with both Python 2 and Python 3:

Python 3 input() function

We will use the sys module to verify the version of Python we are using in each example. Let’s look at the use of the input() function in Python 3:

import sys

# Print Python version 

print(sys.version)

value_string = input("Enter a string value: ")

print(f'Type of value is {type(value_string)}')

value_float = input("Enter a float value: ")

print(f'Type of value is {type(value_float)}')

value_int = input("Enter a integer value: ")

print(f'Type of value is {type(value_int)}')

Let’s run the code to get the result:

3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]
Enter a string value: Python
Type of value is <class 'str'>
Enter a float value: 4.0
Type of value is <class 'str'>
Enter a integer value: 3
Type of value is <class 'str'>

The Python 3 input() function converts all inputs to strings.

Python 2 input() function

import sys

# Print Python version

print(sys.version)

value_string = input("Enter a string value: ")

print 'Type of value is', type(value_string)

value_float = input("Enter a float value: ")

print 'Type of value is', type(value_float)

value_int = input("Enter a integer value: ")

print 'Type of value is', type(value_int)

Let’s run the code to see the result:

2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
Enter a string value: "Python"
Type of value is <type 'str'>
Enter a float value: 4.0
Type of value is <type 'float'>
Enter a integer value: 3
Type of value is <type 'int'>

The Python 2 input() function keeps the type of the input values.

Python 2 raw_input()

The raw_input() function only exists in Python 2 and performs the same functionality as input() in Python 3. Let’s verify this using the following code:

import sys

# Print Python version 

print(sys.version)

value_string = raw_input("Enter a string value: ")

print 'Type of value is', type(value_string)

value_float = raw_input("Enter a float value: ")

print 'Type of value is', type(value_float)

value_int = raw_input("Enter a integer value: ")

print 'Type of value is', type(value_int)

Let’s run the code to see the result:

2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
Enter a string value: "Python"
Type of value is <type 'str'>
Enter a float value: 4.0
Type of value is <type 'str'>
Enter a integer value: 3
Type of value is <type 'str'>

The raw_input() function converts the type of all inputs to string. This Python 2 function was replaced with input() in Python 3. You cannot use raw_input() in Python 3.

Example

Let’s look at an example where we take a number as input from the user and return the square of that number. We will try to use the raw_input() function to collect the input.

import sys

# Print Python version

print(sys.version)

number = int(raw_input("Enter number to square: "))

print(f'{number} squared is {number**2}')

Let’s run the code to see what happens:

3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-aced6fa426ae> in <module>
      2 print(sys.version)
      3 
----> 4 number = int(raw_input("Enter number to square: "))
      5 
      6 print(f'{number} squared is {number**2}')

NameError: name 'raw_input' is not defined

We get the NameError because raw_input() no longer exists as a built-in function in Python 3.

Solution

To solve this error, we need to replace raw_input() with input(). Let’s look at the revised code:

import sys

# Print Python version

print(sys.version)

number = int(input("Enter number to square: "))

print(f'{number} squared is {number**2}')

Note that we have to convert the input to an integer using the int() function because the input() function returns a string.

Let’s run the code to see the correct result:

3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]
Enter number to square: 5
5 squared is 25

We correctly retrieve the input from the user and square the integer value.

Summary

Congratulations on reading to the end of this tutorial! The NameError: name ‘raw_input’ is not defined occurs when you try to call the raw_input() function using Python major version 3. You can only use raw_input() in Python 2.

To solve this error, replace all instances of raw_input() with the input() function in your program.

For further reading on deprecated functions in Python 2, go to the article:

For further reading on NameErrors, go to the articles

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 ✨