1

I'm on Pycharm with python3. I can run the code by shift+control+R (short cut for run, equivalent to press the green triangle run button) Or run the code by shift+alt+E to load the code into Python console suggested by interactive shell debugging with pycharm

shift+control+R gives no errors.

shift+alt+E throws an exception:

TypeError: an integer is required (got type str)

The code I run as follows:

import sys

sys.exit('exist')

print('shouldnt print')

I want to understand what causes the different behavior and how I can avoid this. The code is inline with sys.exit documentation for python3.

Community
  • 1
  • 1
Lisa
  • 4,126
  • 12
  • 42
  • 71

1 Answers1

1

When Shift + Alt + E is pressed, it enters the Interactive shell. sys.exit() doesn't work for IDLE applications such as the Interactive shell. For IDLE applications, the built-in os._exit() is used instead.

When you closely examine the stack trace, you will notice this behavior:

Traceback (most recent call last):
  File "<input>", line 4, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 260, in DoExit 
    os._exit(args[0])

TypeError: an integer is required

os._exit() is executed (instead of sys.exit("exist")), and it takes only an integer as an argument. Check the documentation here: https://docs.python.org/2/library/os.html#os._exit

Abhishek Balaji R
  • 665
  • 10
  • 25
  • How may I know what works for IDLE applications and what doesn't? I encountered similar issue when filter a list. – Lisa Aug 12 '16 at 03:01
  • @Lisa, I am not sure if there are other exceptions. But if you post the problem on SO, maybe someone can take a look and answer. – Abhishek Balaji R Aug 12 '16 at 03:16