By Lonnie Price


2009-06-05 08:32:23 8 Comments

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?

8 comments

@Danijel-James W 2017-08-25 04:50:25

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!

@tacaswell 2015-07-28 21:05:42

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.

@eddy85br 2018-12-13 21:08:39

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.

@ChewbaccaKL 2014-09-22 11:02:21

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.

@Surest Texas 2016-05-16 23:00:41

I get this when I tried it: inputcons = vars(builtins).get('raw_input', input) TypeError: vars() argument must have dict attribute

@jmagnusson 2013-08-20 09:54:29

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

@Babken Vardanyan 2014-08-09 07:08:31

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

@jmagnusson 2014-08-11 09:02:43

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

@Weeble 2012-02-23 11:05:27

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.

@vgoff 2013-08-06 03:48:42

Is IDLE installed with Windows? Running IDLE or idle in my terminal comes up empty.

@Weeble 2013-08-06 09:30:28

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

@vgoff 2013-08-06 09:45:07

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.

@Weeble 2013-08-06 11:15:09

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.

@vgoff 2013-08-06 12:22:02

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.

@Cees Timmerman 2011-09-06 14:59:18

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: "))

@kevlar1818 2013-08-14 19:00:35

I was getting a scope issue with this approach. Fixed it with a global input at the top of the calling function.

@mbarkhau 2013-10-03 16:29:53

This seems a bit cleaner to me: import __builtin__; input = getattr(__builtin__, 'raw_input', input)

@Cees Timmerman 2013-10-04 12:06:08

Python 3 has no module named __builtin__.

@jfs 2014-03-25 19:00:25

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

@Cees Timmerman 2014-03-26 08:55:58

getattr(__builtins__, 'raw_input', input) returns the default when imported in Python 2.7.2.

@Kevin 2015-01-15 00:05:37

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

@jfs 2015-01-15 00:49:36

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

@Efs 2012-02-23 10:31:18

In Python 3.xx you just need input() not raw_input()

@balpha 2009-06-05 08:35:38

raw_input() was renamed to input()

From http://docs.python.org/dev/py3k/whatsnew/3.0.html

@meawoppl 2014-04-09 23:13:43

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.

@Vishnu Narang 2016-03-05 12:22:35

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.

@balpha 2016-03-05 12:25:17

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

@Vishnu Narang 2016-03-05 12:35:02

@balpha : Just verified. You are correct. I have python 2.7 (Can't edit the answer to mention that...)

@dlamblin 2017-09-25 07:17:58

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

@noɥʇʎԀʎzɐɹƆ 2018-07-18 14:57:57

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

Related Questions

Sponsored Content

56 Answered Questions

[SOLVED] Calling an external command in Python

15 Answered Questions

[SOLVED] What are metaclasses in Python?

17 Answered Questions

[SOLVED] How can I make a time delay in Python?

28 Answered Questions

11 Answered Questions

[SOLVED] What are "named tuples" in Python?

42 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

33 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 2647963 View
  • 2315 Score
  • 33 Answer
  • Tags:   python datetime time

17 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

16 Answered Questions

[SOLVED] Convert bytes to a string?

Sponsored Content