608
import sys
print(sys.platform)
print(2**100)
raw_input()

I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for Python 2.5 and I'm using Python 3.1

What should I do to fix this?

Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
Lonnie Price
  • 9,687
  • 7
  • 21
  • 13

8 Answers8

1094

Starting with Python 3, raw_input() was renamed to input().

From What’s New In Python 3.0, Builtins section second item.

HHest
  • 331
  • 3
  • 8
balpha
  • 45,794
  • 14
  • 108
  • 128
  • 57
    There was originally a function input() which acted something like the current eval(input()). It was a leftover from when Python was less security conscious. The change simplified the language. See also "import this" for a deeper explanation. Would you prefer a dead language to one that evolves? There are plenty of those around. – meawoppl Apr 09 '14 at 23:13
  • 11
    I'm learning python as well and found one difference between `input()` and `raw_input()`. `a = input()` will take the user input and put it in the correct type. Eg: if user types 5 then the value in `a` is integer 5. `a = raw_input()` will take the user input and put it as a string. Eg: if user types 5 then the value in `a` is string '5' and not an integer. Thought it might be good info to newbies in python like myself. – Vishnu Narang Mar 05 '16 at 12:22
  • 12
    @VishnuNarang Yep, that is correct in Python 2. But in Python 3, this behavior changed, and input does what raw_input did in Python 2. – balpha Mar 05 '16 at 12:25
  • 5
    @balpha : Just verified. You are correct. I have python 2.7 (Can't edit the answer to mention that...) – Vishnu Narang Mar 05 '16 at 12:35
  • 3
    @meawoppl Moving from python 2 to 3 was meant to be breaking. It doesn't mean it had to be. A language can evolve without dying and without changing the function signature. It helps if you have no global functions. Start out with some hindsight from other languages. Etc. Removing functions is not off the table either. – dlamblin Sep 25 '17 at 07:17
  • 1
    @dlamblin Python was first released in 1991. `input()` was rarely used- a blank space would raise an exception. Python has an automated tool for doing code transforms like these. The function is present to make scripting a little bit easier. Other than shell I/O primitives and fundamental types, python has few global variables. – noɥʇʎԀʎzɐɹƆ Jul 18 '18 at 14:57
124

This works in Python 3.x and 2.x:

# Fix Python 2.x.
try: input = raw_input
except NameError: pass
print("Hi " + input("Say something: "))
Wooble
  • 80,189
  • 12
  • 97
  • 124
Cees Timmerman
  • 13,202
  • 8
  • 78
  • 106
  • 4
    I was getting a [scope issue](http://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value) with this approach. Fixed it with a `global input` at the top of the calling function. – kevlar1818 Aug 14 '13 at 19:00
  • 1
    This seems a bit cleaner to me: `import __builtin__; input = getattr(__builtin__, 'raw_input', input)` – mbarkhau Oct 03 '13 at 16:29
  • 3
    Python 3 has no module named `__builtin__`. – Cees Timmerman Oct 04 '13 at 12:06
  • @mbarkhau: it is too global. What if you call a library that doesn't expect it? `__builtin__` is renamed to `builtins` in Python 3. Both are available as `__builtins__` in CPython. – jfs Mar 25 '14 at 19:00
  • 3
    `getattr(__builtins__, 'raw_input', input)` returns the default when imported in Python 2.7.2. – Cees Timmerman Mar 26 '14 at 08:55
  • @J.F.: 1) All of the changes discussed in this answer/comment thread are local to the current module, though you could globally modify `__builtins__`, I suppose, and 2) if you're calling `input()` in 2.x library code, you have bigger problems. – Kevin Jan 15 '15 at 00:05
  • @Kevin: 1) yes. I've assumed (incorrectly) `builtins` is modified 2) It doesn't matter whether using `input()` is a good idea. It is a bad idea to modify `builtins`. – jfs Jan 15 '15 at 00:49
49

A reliable way to address this is

from six.moves import input

six is a module which patches over many of the 2/3 common code base pain points.

Georgy
  • 6,348
  • 7
  • 46
  • 58
tacaswell
  • 73,661
  • 15
  • 189
  • 183
  • 3
    Good idea... and if you do `from six.moves import input as raw_input`, you can keep `raw_input` in your script and run using both Python versions. – eddy85br Dec 13 '18 at 21:08
16

As others have indicated, the raw_input function has been renamed to input in Python 3.0, and you really would be better served by a more up-to-date book, but I want to point out that there are better ways to see the output of your script.

From your description, I think you're using Windows, you've saved a .py file and then you're double-clicking on it to run it. The terminal window that pops up closes as soon as your program ends, so you can't see what the result of your program was. To solve this, your book recommends adding a raw_input / input statement to wait until the user presses enter. However, as you've seen, if something goes wrong, such as an error in your program, that statement won't be executed and the window will close without you being able to see what went wrong. You might find it easier to use a command-prompt or IDLE.

Use a command-prompt

When you're looking at the folder window that contains your Python program, hold down shift and right-click anywhere in the white background area of the window. The menu that pops up should contain an entry "Open command window here". (I think this works on Windows Vista and Windows 7.) This will open a command-prompt window that looks something like this:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\Users\Weeble\My Python Program>_

To run your program, type the following (substituting your script name):

    python myscript.py

...and press enter. (If you get an error that "python" is not a recognized command, see http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96 ) When your program finishes running, whether it completes successfully or not, the window will remain open and the command-prompt will appear again for you to type another command. If you want to run your program again, you can press the up arrow to recall the previous command you entered and press enter to run it again, rather than having to type out the file name every time.

Use IDLE

IDLE is a simple program editor that comes installed with Python. Among other features it can run your programs in a window. Right-click on your .py file and choose "Edit in IDLE". When your program appears in the editor, press F5 or choose "Run module" from the "Run" menu. Your program will run in a window that stays open after your program ends, and in which you can enter Python commands to run immediately.

Weeble
  • 14,415
  • 3
  • 50
  • 66
  • Is IDLE installed with Windows? Running IDLE or idle in my terminal comes up empty. – vgoff Aug 06 '13 at 03:48
  • 1
    IDLE is installed with Python on Windows. It appears as a shortcut in the start menu and as a file association for .py files, but I don't think there's any 'idle' command added to your path by default. If you want to start it from the command-line, you can run `pythonw -m idlelib.idle` – Weeble Aug 06 '13 at 09:30
  • That explains it. I only touch Windows to make money, not as an OS. I would then say that your "Use IDLE" section is then biased. You should mention the selectivity, perhaps. – vgoff Aug 06 '13 at 09:45
  • 3
    I'm not sure I understand. The question was Windows-specific. It talks about a "DOS popup". IDLE is installed as part of Python, but some distributors may separate it out. For example, in Ubuntu it seems you have to install it as a separate package. The answer is already suggesting a different approach than the questioner asked for, so I'd rather not add further tangents to it. FWIW I use Linux at home and only use Windows where I have to for work. – Weeble Aug 06 '13 at 11:15
  • DOS being an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 including the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition. – vgoff Aug 06 '13 at 12:22
7

Timmerman's solution works great when running the code, but if you don't want to get Undefined name errors when using pyflakes or a similar linter you could use the following instead:

try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass
jmagnusson
  • 5,532
  • 4
  • 41
  • 38
  • Eh, I am still getting lots of errors in pylint: `Redefining built-in 'input' (redefined-builtin)`, `Unable to import '__builtin__' (import-error)`, `Invalid constant name "input" (invalid-name)`. – Babken Vardanyan Aug 09 '14 at 07:08
  • @BabkenVardanyan pylint is notoriously strict when it comes to code style. I would recommend `flake8` or `pyflakes` so as to not take up all your day with meaningless warnings. – jmagnusson Aug 11 '14 at 09:02
6

Here's a piece of code I put in my scripts that I wan't to run in py2/3-agnostic environment:

# Thank you, python2-3 team, for making such a fantastic mess with
# input/raw_input :-)
real_raw_input = vars(__builtins__).get('raw_input',input)

Now you can use real_raw_input. It's quite expensive but short and readable. Using raw input is usually time expensive (waiting for input), so it's not important.

In theory, you can even assign raw_input instead of real_raw_input but there might be modules that check existence of raw_input and behave accordingly. It's better stay on the safe side.

ChewbaccaKL
  • 351
  • 1
  • 3
  • 11
  • I get this when I tried it: inputcons = vars(__builtins__).get('raw_input', input) TypeError: vars() argument must have __dict__ attribute –  May 16 '16 at 23:00
1

Probably not the best solution, but before I came here I just made this on the fly to keep working without having a quick break from study.

def raw_input(x):
  input(x)

Then when I run raw_input('Enter your first name: ') on the script I was working on, it captures it as does input() would.

There may be a reason not to do this, that I haven't come across yet!

Danijel-James W
  • 1,178
  • 2
  • 14
  • 31
1

How about the following one? Should allow you to use either raw_input or input in both Python2 and Python3 with the semantics of Python2's raw_input (aka the semantics of Python3's input)

# raw_input isn't defined in Python3.x, whereas input wasn't behaving like raw_input in Python 2.x
# this should make both input and raw_input work in Python 2.x/3.x like the raw_input from Python 2.x 
try: input = raw_input
except NameError: raw_input = input
George Birbilis
  • 2,295
  • 2
  • 25
  • 33