Python Python Basics (2015) Letter Game App Letter Game Refinement

Siddhartha Kala
Siddhartha Kala
4,170 Points

sys.exit() not working as expected

I am trying to use sys.exit() but it is not exiting python as I was expecting it would. It raises SystemExit exception and then asks for a bunch of options to quit/exit. I am using IPython 6.1.0 on Windows 10. Here is what I am getting:

In [525]: sys.exit() An exception has occurred, use %tb to see the full traceback.

SystemExit

c:\users\siddharthakala\appdata\local\programs\python\python36-32\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)

3 Answers

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 47,025 Points

You should definitely also try Steven's answer, but what happens if you pass 0 to sys.exit()? That should signify a normal exit, so it may not treat it as an exception. I haven't used IPython, so I'm not really sure. I've personally only ever used CPython, the official and most popular implementation, which can be downloaded here

Siddhartha Kala
Siddhartha Kala
4,170 Points

Using CPython is a better option I guess, rather than getting unpredictable output in IPython, or avoiding use of basic commands such as sys.exit() :)

Steven Parker
Steven Parker
177,893 Points

It sounds like there's some differences between "IPython" and regular Python. But what if you just take the suggestion from the message and use exit() or quit() instead of sys.exit()?

Siddhartha Kala
Siddhartha Kala
4,170 Points

Thanks for the suggestion. exit() and quit() work just fine in IPython, also, sys.exit() work fine in normal Python. I was just trying to understand why sys.exit() wont work in IPython. Couldn't find a proper explanation on internet as well. I guess there are some differences in system level command execution in IPython, for example, commands like 'cls' and dir work fine in IPython directly but wont work in normal python, just type cls in Ipython and it will work.

Thanks anyways :)

Siddhartha Kala
Siddhartha Kala
4,170 Points

Tried sys.exit(0) as well as sys.exit(1), but same result (almost). Anyways, I dont much like IPython either.

Here's what I got with sys.exit(0)

In [11]: sys.exit(0) An exception has occurred, use %tb to see the full traceback.

SystemExit: 0

c:\users\siddharthakala\appdata\local\programs\python\python36-32\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)

Michael Hulet
Michael Hulet
Treehouse Moderator 47,025 Points

This definitely seems like an implementation detail of IPython, then. It looks like IPython just doesn't like you using sys.exit instead of exit or quit. CPython doesn't care, and it's also the most common and widely supported Python implementation, as well as the version that Treehouse teaches. I'd recommend sticking with that, but it's awesome you were trying stuff on your own!