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
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.
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?
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!
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!
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.