从内部重新启动 python-script [英] Restart python-script from within itself

查看:35
本文介绍了从内部重新启动 python-script的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Python 的 GTK 应用程序,可以加载多个模块.它从 (linux) 终端运行,如下所示:

I have a python-based GTK application that loads several modules. It is run from the (linux) terminal like so:

./myscript.py --some-flag 设置

用户可以从程序中下载(使用 Git)更新的版本.如果存在/已下载,则会出现一个按钮,我希望使用新编译的内容(包括依赖项/导入)重新启动程序.最好它还使用 sys.argv 的内容重新启动它,以保持所有标志原样.

From within the program the user can download (using Git) newer versions. If such exists/are downloaded, a button appear that I wish would restart the program with newly compiled contents (including dependencies/imports). Preferably it would also restart it using the contents of sys.argv to keep all the flags as they were.

所以我没有找到/需要的是一个很好的重启过程,它会杀死程序的当前实例并使用相同的参数启动一个新的实例.

So what I fail to find/need is a nice restart procedure that kills the current instance of the program and starts a new using the same arguments.

最好该解决方案也适用于 Windows 和 Mac,但这不是必需的.

Preferably the solution should work for Windows and Mac as well but it is not essential.

推荐答案

您正在寻找 os.exec*() 命令族.

You're looking for os.exec*() family of commands.

要使用与最初运行时完全相同的命令行参数重新启动当前程序,您可以使用以下命令:

To restart your current program with exact the same command line arguments as it was originally run, you could use the following:

os.execv(sys.argv[0], sys.argv)

这篇关于从内部重新启动 python-script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆