1

Hi I have troubles using sys.exit in a python console. It works really nice with ipython. My code looks roughly like this:

if name == "lin":
    do stuff
elif name == "static":
    do other stuff
else:
    sys.exit("error in input argument name, Unknown name")

If know the program know jumps in the else loop it breaks down and gives me the error message. If I use IPython everything is nice but if I use a Python console the console freezes and I have to restart it which is kind of inconvenient.

I use Python 2.7 with Spyder on MAC.

Is there a workaround such that I the code works in Python and IPython in the same way? Is this a spyder problem?

Thanks for help

3
  • what happens when you try sys.exit(1) Commented Feb 18, 2016 at 11:38
  • Why do you want to exit from the console? This only makes sense if you execute a program like $ python myprog.py. Commented Feb 18, 2016 at 11:39
  • If I use sys.exit(1) the very same happens but the message is gone. I use this in a function that calculates a complex phase and adds it to data. If now the chosen method is not in the list the program breaks down in order order to prevent calculating further with unphased data. Therefore it would be nice to stop and this point and give the user (or myself) a hint what is the reason Commented Feb 18, 2016 at 11:49

2 Answers 2

2

Not sure this is what you should be using sys.exit for. This function basically just throws a special exception (SystemExit) that is not caught by the python REPL. Basically it exits python, and you go back to the terminal shell. ipython's REPL does catch SystemExit. It displays the message and then goes back to the REPL.

Rather than using sys.exit you should do something like:

def do_something(name):
    if name == "lin":
        print("do stuff")
    elif name == "static":
        print("do other stuff")
    else:
        raise ValueError("Unknown name: {}".format(name))

while True:
    name = raw_input("enter a name: ")
    try:
        do_something(name)
    except ValueError as e:
        print("There was a problem with your input.")
        print(e)
    else:
        print("success")
        break # exit loop
Sign up to request clarification or add additional context in comments.

3 Comments

This works. I have hoped for a less severe change in the code but if I think I will take this one
You don't need the while loop if you're using your code with a REPL. The code will exit with the exception, a stack trace will be printed (including your message) and then the REPL will prompt you for the next command.
A very nice. Thanks alot
0

You need to import sys. The following works for me:

import sys
name="dave"
if name == "lin":
    print "do stuff"
elif name == "static":
    print "do other stuff"
else:
    sys.exit("error in input argument name, Unknown name")

2 Comments

I imported sys. The code works. But the performance in Python console is not nice
Ah apologies I misunderstood the question. @Dunes answer below is excellent imo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.