Software Development

How do I fix the NameError: name 'raw_input' is not defined in Python 3?

KI Asked by Kimberly Harrison · 18-05-2025
0 upvotes 16,294 views 0 comments
The question

I am following an older tutorial for a data entry script, but every time I run the code, I get a "NameError: name 'raw_input' is not defined." I’ve checked my spelling multiple times and it seems correct. Am I missing a specific library import, or has this function been deprecated in the newer versions of Python that I’m using for my development environment?

3 answers

0
DE
Answered on 20-05-2025

The reason you are seeing this error is that raw_input() was a function used in Python 2.x to read strings from the user. In Python 3.x, this function was renamed to simply input(). The old input() function from Python 2, which used to evaluate the input as code, was completely removed for security reasons. To fix your script, simply replace all instances of raw_input() with input(). This is a very common issue when running legacy code or following tutorials written before the industry-wide shift to Python 3. In modern Software Development, staying aware of these version-specific changes is crucial for maintaining cross-compatible codebases.

0
ST
Answered on 21-05-2025

Are you trying to write code that needs to run on both Python 2 and Python 3 simultaneously, or are you planning to strictly use Python 3 moving forward for this project?

CH 22-05-2025

That’s a great point, Steven. If you do need to support both versions for a cross-platform tool, you can add a simple compatibility check at the top of your script. Using try-except to map raw_input to input if the NameError occurs is a standard "shim" technique. This allows your software to detect the interpreter version at runtime and assign the correct function name to a variable, ensuring your user interaction logic doesn't break regardless of whether the client is using an older legacy system or a modern Python 3 environment.

0
JE
Answered on 24-05-2025

In Python 3, input() always returns a string. If you were using raw_input() to get numbers, remember to wrap your new input() call in int() or float() to convert the data type!

KI 25-05-2025

I agree with Jennifer; the type conversion is where most people get stuck after making the switch. In Python 3, the distinction is much cleaner, but you have to be more explicit with your data types!

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session