input() error - NameError: name '...' is not defined
Solution 1
TL;DR
input
function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input
function in Python 2.7, which will not evaluate the read strings.
If you are using Python 3.x, raw_input
has been renamed to input
. Quoting the Python 3.0 release notes,
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing newline stripped. It raisesEOFError
if the input is terminated prematurely. To get the old behavior ofinput()
, useeval(input())
In Python 2.7, there are two functions which can be used to accept user inputs. One is input
and the other one is raw_input
. You can think of the relation between them as follows
input = eval(raw_input)
Consider the following piece of code to understand this better
>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'
input
accepts a string from the user and evaluates the string in the current Python context. When I type dude
as input, it finds that dude
is bound to the value thefourtheye
and so the result of evaluation becomes thefourtheye
and that gets assigned to input_variable
.
If I enter something else which is not there in the current python context, it will fail will the NameError
.
>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined
Security considerations with Python 2.7's input
:
Since whatever user types is evaluated, it imposes security issues as well. For example, if you have already loaded os
module in your program with import os
, and then the user types in
os.remove("/etc/hosts")
this will be evaluated as a function call expression by python and it will be executed. If you are executing Python with elevated privileges, /etc/hosts
file will be deleted. See, how dangerous it could be?
To demonstrate this, let's try to execute input
function again.
>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude
Now, when input("Enter your name: ")
is executed, it waits for the user input and the user input is a valid Python function invocation and so that is also invoked. That is why we are seeing Enter your name again:
prompt again.
So, you are better off with raw_input
function, like this
input_variable = raw_input("Enter your name: ")
If you need to convert the result to some other type, then you can use appropriate functions to convert the string returned by raw_input
. For example, to read inputs as integers, use the int
function, like shown in this answer.
In python 3.x, there is only one function to get user inputs and that is called input
, which is equivalent to Python 2.7's raw_input
.
Solution 2
You are running Python 2, not Python 3. For this to work in Python 2, use raw_input
.
input_variable = raw_input ("Enter your name: ")
print ("your name is" + input_variable)
Solution 3
Since you are writing for Python 3.x, you'll want to begin your script with:
#!/usr/bin/env python3
If you use:
#!/usr/bin/env python
It will default to Python 2.x. These go on the first line of your script, if there is nothing that starts with #! (aka the shebang).
If your scripts just start with:
#! python
Then you can change it to:
#! python3
Although this shorter formatting is only recognized by a few programs, such as the launcher, so it is not the best choice.
The first two examples are much more widely used and will help ensure your code will work on any machine that has Python installed.
Solution 4
I also encountered this issue with a module that was supposed to be compatible for python 2.7 and 3.7
what i found to fix the issue was importing:
from six.moves import input
this fixed the usability for both interpreters
you can read more about the six library here
Solution 5
You should use raw_input
because you are using python-2.7. When you use input()
on a variable (for example: s = input('Name: ')
), it will execute the command ON the Python environment without saving what you wrote on the variable (s
) and create an error if what you wrote is not defined.
raw_input()
will save correctly what you wrote on the variable (for example: f = raw_input('Name : ')
), and it will not execute it in the Python environment without creating any possible error:
input_variable = raw_input('Enter Your Name : ')
print("Your Name Is : " + (input_variable))
Related videos on Youtube
chillpenguin
Updated on March 10, 2022Comments
-
chillpenguin about 2 years
I am getting an error when I try to run this simple script:
input_variable = input("Enter your name: ") print("your name is" + input_variable)
Let's say I type in "dude", the error I am getting is:
line 1, in <module> input_variable = input("Enter your name: ") File "<string>", line 1, in <module> NameError: name 'dude' is not defined
I am running Mac OS X 10.9.1 and I am using the Python Launcher app that came with the install of Python 3.3 to run the script.
-
Kevin over 10 yearsAre you sure it's Python 3.3? I would expect
input
to behave this way, but only in 2.7. What does it say when you runpython --version
from a command prompt? Alternatively, what if you writeimport sys; print(sys.version)
at the beginning of your script? -
Wooble over 10 yearsYou're not running Python 3. You're running Python 2, somehow (I'm not familiar with this "Python Launcher" app)
-
Hyperboreus over 10 yearsPut as first line
import sys
and as second lineprint(sys.version_info)
in order to ascertain which version you are using. -
chillpenguin over 10 yearsI did what Kevin said and it is version 2.7.5! I'm not sure how though. I downloaded and installed version 3.3, in my applications folder there is a folder that is called "Python 3.3" inside that folder there is an app called "Python Launcher" and I am running my scripts by dragging and dropping them onto the Python Launcher app. How come I am still using 2.7 when I am using the 3.3 launcher app?
-
thefourtheye over 10 years@chillpenguin Please include in the question that you are using 2.7. Thats crucial to answer this question.
-
Wooble over 10 years@thefourtheye: The whole question is basically "why is Python Launcher running python 2 instead of, sensibly, Python 3", so... not really.
-
chillpenguin over 10 yearsI just edited my question to include that. I did not know that I was using 2.7.5 until I followed Kevin's instructions.
-
Wooble over 10 years@chillpenguin: check out the Preferences dialog for Python Launcher. Apparently it doesn't default to running the version it was installed with, which is... dumb. (I've never used it myself; I find using the Terminal is better...)
-
chillpenguin over 10 yearsThank you Wooble. Right now under prefences it says "interpreter: /usr/bin/pythonw " Is that what I want to change? What do I change it to?
-
Cosine about 7 years
#!/usr/bin/env python3
as first line
-
-
Spencer Goff over 6 yearsraw_input fixed the issue for me
-
carthurs over 5 yearsBut raw_input() isn't available in Python 3.
-
chepner over 2 yearsIf you need to support Python 2 and Python 3, you should be using the
six
library, which providessix.input
. There's no reason to catch aNameError
every single time you callinput_compatible
if you are using Python 3. -
dux2 over 2 yearsSometimes you don't want to depend on any non standard packages, for example when writing a portable shell script, so this answer has it place and justification.
-
chepner over 2 yearsThat still doesn't excuse using a
try
statement every single time you try to call the function.raw_input
isn't going to suddenly appear while your script is executing; it's either available at the beginning or not at all. Also, if you need compatibility between both versions, there is likely a lot of stuff you are going to need to accommodate, and it doesn't make sense not to usesix
. -
dux2 over 2 yearsPardon @chepner, but I don't agree with you. First, you didn't provide a better alternative for use cases that require a portable solution and cannot use any non-standard package. Since you haven't, I edited my answer to include such. Second, while I normally agree with you that you shouldn't use
try
block for situation you can easily check in advance, in this case, it doesn't have any performance downside and looks cleaner than the alternative in my opinion. -
chepner over 2 yearsNot every problem can or should be solved with the standard library alone.
-
khelwood over 2 years
__builtins__.__dict__.get("input") is None
is never true. You could checkif 'raw_input' in __builtins__.__dict__
. Or you could just checksys.version_info
to see which version of Python you're on. -
dux2 over 2 years@khelwood Thanks, modified it.
-
wjandrea over 2 years
raw_input
is not defined in Python 3. Is that a typo of "Python 2"?