如何在 Python 中切换执行到新脚本?
是否可以让一个脚本调用另一个脚本,然后退出(不知道我是否正确),让另一个脚本运行?具体来说,我想要一个更新脚本来下载 installer.py
然后运行它。由于 installer.py 覆盖了更新脚本,我不能只执行 subprocess.call() ,因为它会在“删除旧安装”阶段失败。
这是可能的还是我必须单独保留更新程序脚本,替换其他所有内容,将新脚本放入临时目录中,然后在下次运行程序时替换它?这被认为是更好的方法吗?
非常感谢你,如果这太明显了,我很抱歉。
Is it possible to have one script call another and then exit (don't know if I'm putting this right), leaving the other script running? Specifically I want an update script to download installer.py
and then run it. Since installer.py
overwrites the update script I can't just do a subprocess.call()
since it will fail at the 'delete old install' stage.
Is this possible or must I instead leave the updater script alone, replace everything else, put the new one in a temporary directory and then replace it next time the program is run? Is this considered a better approach?
Thank you very much and sorry if this is blindingly obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
os.exec*
系列之一函数应该做你想做的事。One of the
os.exec*
family of functions should do what you want.由于您正在下载另一个 Python 脚本,因此您可以尝试使用
exec
来运行它。在 2.x 中,有一个辅助函数execfile
用于此类事情(它包装了exec
关键字的使用;在 3.x 中,您必须读取该文件并通过exec
的结果字符串(如果我没读错的话,它现在是一个函数)在这种情况下,您必须始终 110% 确定您信任该内容!< /strong> 看来这不是问题不过,你。
Since you're downloading another Python script, you could try using
exec
to run it. In 2.x there is a helper functionexecfile
for this sort of thing (which wraps the use of theexec
keyword; in 3.x you must read the file and pass the resulting string toexec
(which is now a function, if I'm reading correctly).You must always be 110% sure you trust the content in cases like this! It seems that this isn't an issue for you, though.