Windows Cmd.exe Window
Larry Bates
lbates at syscononline.com
Thu Jul 7 09:41:56 EDT 2005
Use sys.excepthook to hook a function you define and in that function
print a traceback and pause before exiting. Something like (not tested
but copied from working example):
import sys
def Myexcepthook(type, value, traceback):
print "in Myexcepthook-type=", type," value=",value," traceback=",traceback
import traceback
lines=traceback.format_exception(type, value, traceback)
print "---------------------Traceback lines-----------------------"
print "\n".join(lines)
print "-----------------------------------------------------------"
t=raw_input("Press return to continue")
sys.exit(2)
#
# set sys.excepthook
#
sys.excepthook=Myexcepthook
#
# create an uncaught divide by zero exception
#
a=1/0
-Larry Bates
Giles Brown wrote:
> 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.
>
> Giles
>
More information about the Python-list
mailing list