Lisa
Lisa

Reputation: 4416

Pycharm: different behavior between run and run in Python console?

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.

Upvotes: 1

Views: 1539

Answers (1)

Abhishek Balaji R
Abhishek Balaji R

Reputation: 675

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

Upvotes: 1

Related Questions