1

对于以下代码,如果在命令提示符下运行,结果为: 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()
4

1 回答 1

2

更改我的运行配置以使用 Python 控制台运行脚本对我有用。

打开“运行”菜单(或单击运行按钮左侧的箭头)并单击“编辑配置...”。应显示您的默认配置。在 Configuration->Execution 部分勾选“Run with Python Console”并保存更改。

当使用编辑的运行配置并且重新启动应该可以工作时,您的脚本现在将使用 Python 控制台执行。

于 2019-08-17T12:06:12.107 回答