input() error - NameError: name '...' is not defined
Question: 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?


Answer: **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](https://docs.python.org/3.0/whatsnew/3.0.html#builtins),

> `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`](http://docs.python.org/2/library/functions.html#input) and the other
one is
[`raw_input`](http://docs.python.org/2/library/functions.html#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](http://stackoverflow.com/a/20449433/1903116).

**In python 3.x** , there is only one function to get user inputs and that is
called [`input`](http://docs.python.org/3/library/functions.html#input), which
is equivalent to Python 2.7's `raw_input`.

