views:

1189

answers:

3

I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can analyze my output. How can I keep it open?

+3  A: 

start the script from already open cmd window or at the end of script add

 raw_input("press any key to exit")
Anurag Uniyal
+7  A: 

You have a few options:

  1. Run the program from an already-open terminal. Open a command prompt and type:

    python myscript.py
    

    For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to)

    When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.

  2. Add code to wait at the end of your script. Adding ...

    raw_input()
    

    ... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts.

  3. Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particulary useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.

nosklo
+1 to item 3. I use Scite as my Python editor and it simply keeps the output in its own output window. This is really useful because you can potentially see the output of more than one run in a single window.
MadKeithV
I am able to see the output by running it from an already open terminal. But I need to give the complete script address in the python command? Can that be avoided?When I use the raw_input() method, it gives me NameError: name 'raw_input'is not defined. Can you suggest an editor which automatically pauses after execution?
movingahead
@movingahead: maybe you are using python 3? in python 3 it was renamed to input(). But I would use python 2.6 for now, since python 3 lacks important third party libraries that haven't been ported yet. (see other questions on python 2vs3). About editor, I don't use windows, notepad++ lets you configure the command. I use emacs which has a windows version, but I never used it.
nosklo
@nosklo thanks!! Yah I am using Python 3. Doing basic stuff now so shouldn't be much of an issue. I will try notepad++. Am currently trying out Pydev for Eclipse
movingahead
+1  A: 

cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog.

ΤΖΩΤΖΙΟΥ
yah it works out too
movingahead