Windows Cmd.exe Window

Thorsten Kampe thorsten at thorstenkampe.de
Thu Jul 7 09:08:23 EDT 2005


* Giles Brown (2005-07-07 13:56 +0100)
> For my sins I'm a MS Windows user at work and apart from that I have a
> small problem ...
> 
> I like to write python scripts to do small tasks and then double click
> on them from the file explorer to run them.
> 
> Unfortunately I'm not perfect and sometimes I make mistakes and have
> unhandled exceptions or syntax errors when running the scripts.  The
> default behaviour is to shut down the command window which leaves you
> no chance of reading the exception.
> 
> In the past I have created .bat wrapper files that just call the python
> interpreter, but it is a bit tedious to have to create a matching .bat
> file for every script.  So I came up with the following approach...
> 
> 1. Copy python.exe to pythoncmd.exe
> 2. Add a bit of stuff to sitecustomize.py
> 3. Add a special first line to every python script and give it a .cmd
> extension.
> 
> The stuff added to sitecustomize.py (actually I created a
> sitecustomize.py for this) is:
> """
> import sys
> import os
> 
> if os.path.basename(sys.executable) == 'pythoncmd.exe':
> 
>     def cmdexcepthook(*args):
>         sys.__excepthook__(*args)
>         # Let use confirm/inspect error
>         os.system('pause')
> 
>     sys.excepthook = cmdexcepthook
> """
> 
> The special first line is:
> 
> @pythoncmd -x "%~f0" %* & exit /b
> 
> (In the python.org FAQ for windows it says
> @setlocal enableextensions & python -x %~f0 %* & goto :EOF
> but since I have no idea which is "right" I chose the simpler looking
> one)
> 
> This approach does require pythoncmd.exe to by in your %PATH% but I
> think that is reasonable ;)
> 
> I am a bit disappointed I couldn't think of a way of deciding if I was
> running a ".cmd" file in sitecustomize.py so that I could just use the
> normal python.exe.  Using a renamed interpreter .exe is just a trick
> for detecting when I am running .cmd files, but it means that the
> script won't run on another machine that hasn't had the python.exe
> copied to pythoncmd.exe on it.  Which is a shame.
> 
> So my question.  Is there a better way?  I'm not really happy with this
> approach.  Should I stop worrying and go and play my new ukulele?
> Answers please.

I think it's a FAQ to: use raw_input at the end to prevent closing.



More information about the Python-list mailing list