Using python on Windows

We’ve found that some of us have had trouble using Python on Windows.  There have been two problems: first, using an easy text editor.  Second, getting your command window to stay open.  Here’s how to do both, below the fold.The Programming Historian 2 suggests Komodo Edit.  Admittedly, this is a powerful editor, and free (as in code, love, and beer).  But the interface is clunky, and it takes setting up.  Instead, try Notepad2, which you can download here, and it, too, is gratis.  It’s very spare, which is actually what we want at this stage.  Remember when you’re using it to save python programs with the .py extension, and that you have to save any changes in order for them to take effect once you want to run a program.  And once something is saved, to run it, select File > Launch > Execute document, and voila, it runs.

The disappearing output window is, unfortunately, a tougher nut to crack.  Do some searches for this problem on the intergoogle, and you’ll find that it’s not just us.  The issue is that windows just executes the program, prints the information, and then figures it’s done.  That’s not a python issue; it happens with any programming language.  Ugh.

One solution that may work for you is to openthe command line environment as an administrator, and then execute your program from there or notepad2 (although admittedly, neither worked for me on a windows 7 machine).  From the Start menu, search for cmd.exe, right-click it, and open as administrator.  Then run your program.  As I said, it didn’t work for me.

What did work for me was building into my python code a way to keep that window open as long as I need it.  I did that by including a prompt for input at the beginning and end of the program I’ve written.  Now when it runs, my command window opens and prompts me for input.  I can type in anything I want, and hit Enter.  Then the rest of my program executes, and then the prompt that I’ve included at the end of the program appears.  In between is whatever printed output my program has generated.  When I’m done reading it, I can enter any text (it doesn’t matter, it’s just there to keep the window open), and hit Enter, and the window disappears.

The method (“method” meaning python function) I use to do this is “raw_input”, which asks the user for input (see what I mean by python being a high-level language, with methods that are almost recognizable by non-programmers?).  I set a dummy variable that in this case I’ve called “keepwindowopen”, ask for input, and the window must wait for you, like this:

keepwindowopen = raw_input (‘–> ‘)

Here’s how I’ve used it in one of our Programming Historian python routines,

keepwindowopen = raw_input(‘–> ‘)
f = open(‘helloworld.txt’,’r’)
message =
print message
keepwindowopen = raw_input(‘–> ‘)

I know that this is a bit of a pain to insert this at the beginning and end of the program, but it’s the least difficult workaround I’ve found.

Happy python, and happy spring break.

One thought on “Using python on Windows”

Comments are closed.