NameError: name 'raw_input' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: name 'raw_input' is not defined in Python #

The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input() function in Python 3. To solve the error, use the input() function instead of raw_input in Python 3 applications, e.g. s = input('Your name: ').

nameerror name raw input is not defined

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'raw_input' is not defined s = raw_input('Your name: ') print(s)

The raw_input function has been renamed to input in Python 3.

To solve the error replace calls to raw_input with input in your code.

main.py
# ✅ using input() instead of raw_input() s = input('Your name: ') print(s)

using input function python

Make sure to replace all calls to the raw_input() function with input() in Python 3.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

An alternative to providing the prompt argument is to use calls to the print() function.

main.py
print('Your name: ') s = input() print(s)

using input function print

This is very similar to passing the prompt argument to the input() function but instead of showing the prompt on the same line, it is displayed on a separate line.

Conclusion #

The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input() function in Python 3. To solve the error, use the input() function instead of raw_input in Python 3 applications, e.g. s = input('Your name: ').

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.