By chillpenguin

2014-01-14 19:44:14 8 Comments

I am getting an error when I try to run this simple python script:

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

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

Edit: I realized I am somehow running these scripts with 2.7. I guess the real question is how do I run my scripts with version 3.3? I thought if I dragged and dropped my scripts on top of the Python Launcher app that is inside the Python 3.3 folder in my applications folder that it would launch my scripts using 3.3. I guess this method still launches scripts with 2.7. So How do I use 3.3?


@Hardian 2019-02-06 15:53:38

We are using the following that works both python 2 and python 3

#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Enter your name: "))

@RAHUL KUMAR 2018-02-23 06:36:21

For python 3 and above

s = raw_input()

it will solve the problem on pycharm IDE if you are solving on online site exactly hackerrank then use:

s = input()

@Parminder Singh 2018-07-14 08:20:10

input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)

You have to enter input in either single or double quotes

Ex:'dude' -> correct

    dude -> not correct

@Abdullah Baraka 2017-10-29 17:32:07

You could either do:

x = raw_input("enter your name")
print "your name is %s " % x


x = str(input("enter your name"))
print "your name is %s" % x

@Spencer Goff 2017-12-08 17:14:52

raw_input fixed the issue for me

@carthurs 2018-09-03 17:06:22

But raw_input() isn't available in Python 3.

@Skiller Dz 2018-05-06 19:45:56

you should use raw_input because you are using python-2.7 , When you use input() on a variable example : s = input('Name: ') , it will execute the Command ON the Python Environement Without Saving What you Wrote on the variable (s) and creating an error If What you wrote is not Definied , So , You May Use raw_input() , it will save correctly what you wrote on the variable , example : f = raw_input('Name : ') and it will not Execute it on the python environement without Create Any Possible Error

input_variable = raw_input('Enter Your Name : ')
print("Your Name Is  : " + (input_variable))

@Maximilian Peters 2018-05-06 21:14:37

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

@Skiller Dz 2018-05-06 21:37:31

A lot Of Who Answred to This Post Didn't Make an Explanation and Assumptions So why I Need to Make it ? when you use input() in Python 2.7 , it will Apply Automotquly What you Writted On The Terminal , It Will not Save It on your variable , But , raw_input will save It , exampe f = input('Name : ') , there it will Execute the command on the environement directly with creating an error if what you Choosed are not definied , but f = raw_input('Name : ') , raw_input will save what you writted on f variable and will not be executed , not Like input() that are my explanation

@Maximilian Peters 2018-05-06 21:45:24

Thanks for adding the explanation to the answer! Copy&pasting might be a short term solution, explanations make the site useful.

@Cleve Green 2017-10-27 13:00:53

You can change which python you're using with your IDE, if you've already downloaded python 3.x it shouldn't be too hard to switch. But your script works fine on python 3.x, I would just change

print ("your name is" + input_variable)


print ("your name is", input_variable)

Because with the comma it prints with a whitespace in between your name is and whatever the user inputted. AND: if you're using 2.7 just use raw_input instead of input.

@automonous 2017-01-18 19:21:02

For anyone else that may run into this issue, turns out that even if you include #!/usr/bin/env python3 at the beginning of your script, the shebang is ignored if the file isn't executable.

To determine whether or not your file is executable:

  • run ./ from the command line
  • if you get -bash: ./ Permission denied, run chmod a+x
  • run ./ again

If you've included import sys; print(sys.version) as Kevin suggested, you'll now see that the script is being interpreted by python3

@Zim 2016-08-09 00:09:32

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.

@user2534732 2014-01-14 19:53:13

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)

@thefourtheye 2014-01-14 20:00:05


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 to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(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

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


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.

@PopularIsn'tRight 2014-11-06 03:03:51

I know this question is a dup which is why there are so few votes here, but this is a fantastic answer. You explain the theory, give a practical example of something we newbies might not have thought of, and give a simple solution. Thank you.

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] input string in python 3

10 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 539017 View
  • 1349 Score
  • 10 Answer
  • Tags:   python object

10 Answered Questions

11 Answered Questions

[SOLVED] How to remove border (outline) around text/input boxes? (Chrome)

2 Answered Questions

[SOLVED] python 3 NameError

4 Answered Questions

[SOLVED] int(input()) error - NameError: name '…' is not defined

  • 2018-06-20 15:24:06
  • Jonathan
  • 121 View
  • 1 Score
  • 4 Answer
  • Tags:   python python-3.x

2 Answered Questions

NameError: name 'urlstring' is not defined

  • 2017-10-08 06:18:13
  • Jagannatha Mv
  • 628 View
  • -2 Score
  • 2 Answer
  • Tags:   python python-2.7

2 Answered Questions

[SOLVED] NameError: name '' is not defined

  • 2013-12-13 04:09:59
  • user1930797
  • 1944 View
  • 1 Score
  • 2 Answer
  • Tags:   python nameerror

3 Answered Questions

[SOLVED] Python - NameError: name 'input' is not defined

1 Answered Questions

[SOLVED] Name error name is not defined

  • 2015-07-25 03:09:13
  • qwerty77asdf
  • 433 View
  • -2 Score
  • 1 Answer
  • Tags:   python python-3.3

Sponsored Content