Python is easy?

Bengt Richter bokr at
Thu Aug 15 08:58:58 CEST 2002

On Thu, 15 Aug 2002 04:31:20 GMT, "Patrick Ellis" <pellis at> wrote:

>Double clicking a *.py file in explorer will also run the script, as above.
>Again, just like a .exe. The drawback is that the DOS shell that appears,
>will disappear as soon as the program exits, whether normally or due to an
>error. It would be nice if someone out there knew how keep the window up
>after exit, especially due to a compile error. I use sys.stdin.readline() at
>the end of many scripts to force a CR to end the script and close the
>window, but this doesn't help the error cases.
Oops, I misread what you wanted to do. I.e., I thought you wanted to fix the start
menu shortcut so that a command window remains when you exit python.

The double click goes through a file association, which you can modify in various
ways depending on the platform. NT has command line assoc and ftype commands to do it,
as well as doing it via regedt32. The latter will let you set up a right-click menu
item that you can use with the explorer to run a .py script in a persistent console
window. I just tried it. The working directory seems to get set to wherever the .py
file is that you access that way, which seems reasonable and useful.

Here's a recipe to give you a right-click popup menu item called "Run in Persistent Console"
in the explorer for NT4:

run regedt32
go to the HKEY_CLASSES_ROOT window and walk down the keys to Python.File
expand the subkey tree so you can see it has

        Edit with IDLE
                Data: D:\Python22\pythonw.exe D:\Python22\Tools\idle\idle.pyw -e "%1"

                Data: D:\Python22\python.exe "%1" %*

Then add this under shell at the same level as Edit with Idel and open:

        Run in Persistent Console
                Data: cmd.exe /x /k D:\Python22\python.exe "%1"

I.e., highlight shell and select edit>add key... from the menu, and give it the name
"Run in Persistent Console" & never mind the class. Give it a moment to update.

Then select the subkey you just created, and repeat to add a subkey named "command" under it.
Then select the command subkey you just created and got to the menu and select edit>add value.
Never mind the name, just click ok. Then in the string slot enter
    cmd.exe /x /k D:\Python22\python.exe "%1"
and click ok and you're done. Let it update, and then go right click on a .py file with the
explorer and you should be able to select "Run in Persistent Console" from the popup menu.
When the script finishes and python exits, the console window should be there, showing the
last windowful of output. I think you should be able to modify the console window properties
to have a wider window and a long buffer to catch more output. I haven't done it in this context,
but I have in my regular comsole window, and it persists if you so specify, and it's handy.

Aha. It does work ;-) It's apparently keyed on the window title

The last added subtree printed as (leaving out write times):

Key Name:          Python.File\shell\Run in Persistent Console
Class Name:        <NO CLASS>
Key Name:          Python.File\shell\Run in Persistent Console\command
Class Name:        <NO CLASS>
Value 0
  Name:            <NO NAME>
  Type:            REG_SZ
  Data:            cmd.exe /x /k D:\Python22\python.exe "%1"

You could add %* like on the open command, but I don't know where command
line args would come from in this context, though I could see a way to rig
a prompt for args and passing those.

Anyway, this leaves the old double-click as is and just gives you a right-click
menu option.

Thanks for asking, or I wouldn't have gotten this set up for myself ;-)

If you want the double click to act that way, then you could just modify the command
line for it the same way, prefixing cmd.exe /x /k (I like the /x extensions, but /k
is all you need to keep the console window).

Bengt Richter

More information about the Python-list mailing list