简体   繁体   English

如何在Pycharm中重新启动程序?

[英]How to restart a program in Pycharm?

For the following code, if it is run in command prompt, the result is: 1 2 3 4 5 6 1 2 3 4 5 6 . 对于以下代码,如果在命令提示符下运行,则结果为:1 2 3 4 5 6 1 2 3 4 5 6。 . .

If it is run in Pycharm, the result is only: 1 2 3 4 5 6. That is, restart_program() doesn't produce anything in Pycharm. 如果在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()

Changing my run configuration to run the script with Python Console did the trick for me. 更改运行配置以使用Python控制台运行脚本对我来说很成功。

Open the "Run" menu (or click the arrow to the left of the run button) and click "Edit Configurations...". 打开“运行”菜单(或单击运行按钮左侧的箭头),然后单击“编辑配置...”。 Your default configuration should be displayed. 您的默认配置应显示。 In the Configuration->Execution section tick "Run with Python Console" and save the changes. 在“配置”->“执行”部分中,选中“使用Python控制台运行”,然后保存更改。

Your script will now be executed with the Python Console when using the edited run configuration and restarting should work. 现在,当使用编辑后的运行配置并重新启动后,您的脚本将通过Python控制台执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM