Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
By Lonnie Price
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?
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.
Then when I run raw_input('Enter your first name: ') on the script I was working on, it captures it as does input() would.
raw_input('Enter your first name: ')
There may be a reason not to do this, that I haven't come across yet!
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.
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.
from six.moves import input as raw_input
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.
I get this when I tried it: inputcons = vars(builtins).get('raw_input', input) TypeError: vars() argument must have dict attribute
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:
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
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).
Redefining built-in 'input' (redefined-builtin)
Unable to import '__builtin__' (import-error)
Invalid constant name "input" (invalid-name)
@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.
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.
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):
...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.
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.
Is IDLE installed with Windows? Running IDLE or idle in my terminal comes up empty.
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
pythonw -m idlelib.idle
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.
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.
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.
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: "))
I was getting a scope issue with this approach. Fixed it with a global input at the top of the calling function.
This seems a bit cleaner to me: import __builtin__; input = getattr(__builtin__, 'raw_input', input)
import __builtin__; input = getattr(__builtin__, 'raw_input', input)
Python 3 has no module named __builtin__.
@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.
getattr(__builtins__, 'raw_input', input) returns the default when imported in Python 2.7.2.
getattr(__builtins__, 'raw_input', input)
@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: 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.
In Python 3.xx you just need input() not raw_input()
raw_input() was renamed to input()
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.
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.
a = input()
a = raw_input()
@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 : Just verified. You are correct. I have python 2.7 (Can't edit the answer to mention that...)
@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 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.