Fix - NameError: name 'raw_input' is not defined in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20221 min read

banner

Photo by Raychan

Fix - 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.

Use the search field on my Home Page to filter through my more than 1,000 articles.