Nameerror name ‘raw_input’ is not defined

Struggling to fix the Python nameerror name ‘raw_input’ is not defined error message?

Errors are inevitable, and it’s frustrating when you don’t know how to resolve them.

Well, in this article, we are going to show you how you are going to fix this error.

We will also discuss what this error is all about and why it occurs in your Python script.

What is “nameerror name ‘raw_input’ is not defined”?

The error message “nameerror: name ‘raw_input’ is not defined occurs when you try to call the raw_input() function using Python version 3.

Unfortunately, the raw_input() function has been renamed into input() in Python version 3.

So that’s why when you are trying to call raw_input() it raised an nameerror name ‘raw_input’ is not defined, because the function does not exist in that version of Python.

You can only use raw_input() in Python version 2, raw_input function is used to accept the input from the user.

Why does “python raw_input not defined” error message occur?

There are several reasons why this error occurs in your Python script, here are the following:

❌ Incorrect Function Name

❌ Incorrect Syntax

❌ Typing Error

❌ Incorrect Module

How to fix “nameerror name ‘raw_input’ is not defined”?

To fix this error, you have to replace all instances of raw_input() with the input() function in your program

The following are the solutions that you can use to resolve this error.

Solution 1: Change the raw_input() function into input()

As mentioned above, the raw_input() function has been removed in Python version 3 into the input() function. Therefore, you have to change or replace all instances of raw_input().

Incorrect code:

❌website = raw_input("Enter your favorite website: ")

Corrected code:


✅ website = input("Enter your favorite website: ")

For example:

✅ website = input("Enter your favorite website: ")
print("Hello, welcome to " + website + "!")

Output:

Enter your favorite website: Itsourcecode
Hello, welcome to Itsourcecode!

Example code using input in mathematical computation

✅ a = input('Enter First Number: ')
b = input('Enter Second Number: ')

result = int(a) + int(b)
print('Total: ', result)

Output:

Enter First Number: 5
Enter Second Number: 5
Total:  10

Solution 2: Use six library

When you have a lot of legacy code that uses raw_input() and you don’t want to replace all instances with input(), you can use a compatibility library like six.

This library provides a wrapper function called input() that works like the old raw_input() function.

For example:

 import six

website = six.moves.input("Enter your favorite website: ")
print("Hello,welcome to " + website + "!")

Output:

Enter your favorite website: itsourcode
Hello,welcome to itsourcode!

Solution 3: Use Aliases

You can define an alias for raw_input() and map it to input() using the built-in function setattr().

This will allow you to use raw_input() in your code, but it will actually call input().

For example:

setattr(__builtins__, 'raw_input', input)

website = raw_input("Enter your favorite website:")
print("Hello, welcome to " + website + "!")

Output:

Enter your favorite website:Itsourcecode
Hello, welcome to Itsourcecode!

Conclusion

The error message “nameerror: name ‘raw_input’ is not defined occurs when you try to call the raw_input() function using Python version 3.

This article already provides solutions for this error to help you fix the Python name model is not defined error message.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python NameError and what causes it?

NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.

How do I fix ‘name X is not defined’?

Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.

Why does my variable work in one cell but not another (Jupyter)?

Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.

What is the difference between NameError and AttributeError?

NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.

Where can I find more NameError fixes?

Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →