Keeping console window open

F

Fencer

Hello, I need to write a simple utility program that will be used under
Windows. I want to write the utility in python and it will be run by
double-clicking the the .py-file.

I put a raw_input('Press enter to exit) at the end so the console window
wouldn't just disappear when the program is finished.

Anyway, I wrote a few lines of code and when I first tried to run it by
double-clicking the .py-file the console window still disappeared right
away. So, in order to see what was happening, I ran it from a shell and
it turned out to be a missing import. My question is how can I trap
errors encountered by the interpreter (if that is the right way to put
it) in order to keep the console window open so one has a chance to see
the error message?

- Fencer
 
T

Tomasz Zieliñski

My question is how can I trap
errors encountered by the interpreter (if that is the right way to put
it) in order to keep the console window open so one has a chance to see
the error message?

Interpreter errors are same beasts as exceptions,
so you can try:...except: them.
 
F

Fencer

Scott said:
To be a trifle more explicit, turn:

...
if __name__ == '__main__':
main()

into:
...
if __name__ == '__main__':
try:
main()
except Exception, why:
print 'Failed:', why
import sys, traceback
traceback.print_tb(sys.exc_info()[2])
raw_input('Leaving: ')

Note that building your script like this also allows you to
open the interpretter, and type:
import mymodule
mymodule.main()
in order to examine how it runs.

Thanks alot, this was exactly what I was looking for!
 
D

Dave Angel

Fencer said:
To be a trifle more explicit, turn:

...
if __name__ == '__main__':
main()

into:
...
if __name__ == '__main__':
try:
main()
except Exception, why:
print 'Failed:', why
import sys, traceback
traceback.print_tb(sys.exc_info()[2])
raw_input('Leaving: ')

Note that building your script like this also allows you to
open the interpretter, and type:
import mymodule
mymodule.main()
in order to examine how it runs.

Thanks alot, this was exactly what I was looking for!
--Scott David Daniels
(e-mail address removed)

</div>
Notice also that you'd then move all the imports inside main(), rather
than putting them at outer scope. Now some imports, such as sys and os,
may want to be at outer scope, but if they don't work,. your system is
seriously busted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top