Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Stop Python from exiting upon error in Windows

Reply
Thread Tools

Re: Stop Python from exiting upon error in Windows

 
 
Tom Plunket
Guest
Posts: n/a
 
      07-15-2003
Robert wrote:

> How can I stop the Python interpreter from exiting when an error occurs?


create a batchfile, tell Windows that the association of Python
files is to that batch file, and put this in the file:

python.exe %1
pause


Or- catch the error in your mainline, and do a sys.raw_input()
call on exception.

-tom!
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      07-15-2003
Can you nest exceptions? I already have a few legitimate try/except blocks
where I know there's a chance the act could go bad, do I just stick a try:
at the beginning of the whole body of code and an except: at the end?

- Robert

"Tom Plunket" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Robert wrote:
>
> > How can I stop the Python interpreter from exiting when an error occurs?

>
> create a batchfile, tell Windows that the association of Python
> files is to that batch file, and put this in the file:
>
> python.exe %1
> pause
>
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.
>
> -tom!



 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      07-15-2003
Tom Plunket wrote:
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.


Tom meant just "raw_input()", which is a builtin, rather than
sys.raw_input which does not exist, of course.

To answer your question in the other reply, yes, you can
nest exceptions. If you have a try/except and the raw_input
in the except, however, you won't see any exception traceback
printed at the console so you'll need something like the
traceback module and one of the functions from it, like
traceback.print_exc().

-Peter
 
Reply With Quote
 
Conrad
Guest
Posts: n/a
 
      07-15-2003
Years ago, Nostradamus predicted that on Mon, 14 Jul 2003 20:06:14 -0700,
Tom Plunket would write, saying:

> Robert wrote:
>
>> How can I stop the Python interpreter from exiting when an error occurs?

>
> create a batchfile, tell Windows that the association of Python
> files is to that batch file, and put this in the file:
>
> python.exe %1
> pause
>
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.
>
> -tom!


Or the third, and admittedly brute force solution
I use is to fire up the DOS shell, (click on START,
then RUN, then type "command"). Depending on which
Win you're running, you may want to run DOSKEY,
which lets you cursor back up to previous commands.

Once you've got the command window up (and doskeyed),
cd to your python source directory, and type in
something like *C:\python22\python.exe mypythonfile.py*
(leave out the *s and be sure python is in the same
place on your machine.)

This doesn't keep the python interpreter from exiting,
but it does keep the DOS window open to let you see
your error messages.

I admit it's ugly, but hey, I mostly develop in
FreeBSD and Linux, where the CLI is your buddy

Conrad


 
Reply With Quote
 
Guest
Posts: n/a
 
      07-15-2003
I hear ya. That's what I'm doing at the moment to spot something, but I
wanted to "polish" the program a little...

- Robert

"Conrad" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> Years ago, Nostradamus predicted that on Mon, 14 Jul 2003 20:06:14 -0700,
> Tom Plunket would write, saying:
>
> > Robert wrote:
> >
> >> How can I stop the Python interpreter from exiting when an error

occurs?
> >
> > create a batchfile, tell Windows that the association of Python
> > files is to that batch file, and put this in the file:
> >
> > python.exe %1
> > pause
> >
> >
> > Or- catch the error in your mainline, and do a sys.raw_input()
> > call on exception.
> >
> > -tom!

>
> Or the third, and admittedly brute force solution
> I use is to fire up the DOS shell, (click on START,
> then RUN, then type "command"). Depending on which
> Win you're running, you may want to run DOSKEY,
> which lets you cursor back up to previous commands.
>
> Once you've got the command window up (and doskeyed),
> cd to your python source directory, and type in
> something like *C:\python22\python.exe mypythonfile.py*
> (leave out the *s and be sure python is in the same
> place on your machine.)
>
> This doesn't keep the python interpreter from exiting,
> but it does keep the DOS window open to let you see
> your error messages.
>
> I admit it's ugly, but hey, I mostly develop in
> FreeBSD and Linux, where the CLI is your buddy
>
> Conrad
>
>



 
Reply With Quote
 
Guest
Posts: n/a
 
      07-15-2003
Ok, I figured out the IDLE block indent (duh - RTFMenu), and now it works
great in DOS. Again, thanks to all who helped.

However, I'd still like to know how to determine what environment I'm
running under inside my code. Any suggestions?

- Robert

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks! I'm getting closer to what I want, but it raises two questions:
>
> 1. Is there an IDLE keystroke to indent a block of code? (just putting

try:
> at the start of my program causes an error, expecting an indented block to
> follow which is my entire program!)
>
> 2. Is there a way to tell the environment I'm running under (python
> interpreter, IDLE window, or other)? I'd like to put a pause at the end of
> my program if and only if I'm running under the python.exe DOS-like
> program...
>
> - Robert
>
> "Peter Hansen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Tom Plunket wrote:
> > >
> > > Or- catch the error in your mainline, and do a sys.raw_input()
> > > call on exception.

> >
> > Tom meant just "raw_input()", which is a builtin, rather than
> > sys.raw_input which does not exist, of course.
> >
> > To answer your question in the other reply, yes, you can
> > nest exceptions. If you have a try/except and the raw_input
> > in the except, however, you won't see any exception traceback
> > printed at the console so you'll need something like the
> > traceback module and one of the functions from it, like
> > traceback.print_exc().
> >
> > -Peter

>
>



 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-15-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Ok, I figured out the IDLE block indent (duh - RTFMenu), and now it works
> great in DOS. Again, thanks to all who helped.
>
> However, I'd still like to know how to determine what environment I'm
> running under inside my code. Any suggestions?


I doubt if there's any particular way to do it consistently. In
principle one could write a cute module which would be able to check
the "signature" of the environment to figure it all out. Check if
certain modules are in __builtins__ to see if you are running inside
an application that uses Python for scripting, check for sys.argv[0]
to learn whether you were launched in a way that suggests running as
a script with python.exe, etc.

It's probably not a great idea to write anything which depends on this
however, and it sounds like more time than it's worth.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stop script w/o exiting interpreter Alan Isaac Python 6 01-28-2007 01:13 AM
How do I stop Python IDLE (GUI) from immediately exiting when I enter it? John (Z R) L Python 6 11-21-2006 07:17 PM
Problem exiting application in Windows Console. Ant Python 6 11-09-2006 12:02 PM
How to stop perl from exiting when stat($_)->size fails Peter Juuls Perl Misc 8 02-16-2006 10:31 AM
exiting Dos program hangs windows fhg A+ Certification 3 01-22-2004 09:16 PM



Advertisments