Pycharm:在Python控制台中运行和运行的行为不同?

Pycharm:在Python控制台中运行和运行的行为不同?,python,pycharm,Python,Pycharm,我在用python3玩Pycharm。 我可以通过shift+control+R运行代码(运行的捷径,相当于按下绿色三角形的运行按钮) 或 通过shift+alt+E运行代码,将代码加载到 shift+control+R不会出现错误 shift+alt+E引发异常: TypeError: an integer is required (got type str) 我运行的代码如下所示: import sys sys.exit('exist') print('shouldnt print')

我在用python3玩Pycharm。 我可以通过shift+control+R运行代码(运行的捷径,相当于按下绿色三角形的运行按钮) 或 通过shift+alt+E运行代码,将代码加载到

shift+control+R不会出现错误

shift+alt+E引发异常:

TypeError: an integer is required (got type str)
我运行的代码如下所示:

import sys

sys.exit('exist')

print('shouldnt print')

我想了解是什么导致了不同的行为,以及如何避免这种情况。代码与python3的
sys.exit
文档内联。

当按下Shift+Alt+E时,它进入交互式shell
sys.exit()
对于空闲应用程序(如交互式shell)不起作用。对于空闲应用程序,使用内置的
os.\u exit()

仔细检查堆栈跟踪时,您会注意到以下行为:

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
回溯(最近一次呼叫最后一次):
文件“”,第4行,在
DoExit中的文件“/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py”,第260行
os.\u退出(参数[0])
TypeError:需要一个整数

执行
os.\u exit()
(而不是
sys.exit(“exist”)
),它只接受一个整数作为参数。查看此处的文档:

我如何知道哪些适用于空闲应用程序,哪些不适用?我在筛选列表时遇到了类似的问题。@Lisa,我不确定是否还有其他异常。但是如果你把问题贴在上面,也许有人可以看一下并回答。