problems exiting from Python using iPython/Spyder

Viewed 13883

This question has been asked before, but I have tried the solutions in related questions such as this to no avail.

I am having problems with Python's exit command, and I have ruled out a problem with my code as run by vanilla Python 3. The problem comes when I run it with iPython or in Spyder's iPython console.

When I use just a simple exit command, I get the error:

NameError: name 'exit' is not defined

I have already imported sys as suggested by the other link. The only thing that kind of works is to try sys.exit() in which case I get:

An exception has occurred, use %tb to see the full traceback.

SystemExit

C:\Users\sdewey\AppData\Local\Continuum\Anaconda3\lib\site-
packages\IPython\core\interactiveshell.py:2870: UserWarning: To exit: use 
'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I only say that that "kind of works" because the error message is smaller so it's less annoying :).

Any ideas? Seems like an issue with iPython. I have encountered a different issue in Jupyter (which uses iPython) where quit was ignored entirely, which I posted about separately here

2 Answers

You can use system warnings to set those warning that you do not need as ignored. Example: the function that you call from somewhere else:

def my_function:
  statements

                    
    if (something happened that you want to exit):
        
        import warnings
        warnings.filterwarnings("ignore")
        sys.exit('exiting...')

Related