对于以下代码,如果在命令提示符下运行,则结果为:
1个
2
3
4
5
6
1个
2
3
4
5
6
。
。
。
如果在Pycharm中运行,则结果仅为:
1个
2
3
4
5
6.也就是说,restart_program()在Pycharm中不产生任何东西。
import sys
import os
def restart_program():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
python = sys.executable
os.execl(python, python, *sys.argv)
if __name__ == "__main__":
for i in range(1,10,1):
print i
if i>5:
restart_program()
最佳答案
更改运行配置以使用Python控制台运行脚本对我来说很成功。
打开“运行”菜单(或单击运行按钮左侧的箭头),然后单击“编辑配置...”。
您的默认配置应显示。在“配置”->“执行”部分中,选中“使用Python控制台运行”,然后保存更改。
现在,当使用编辑后的运行配置并重新启动后,您的脚本将通过Python控制台执行。