如何远程更新Python应用程序
将更改推送到用 Python 编写的程序的最佳方法是什么?我有一个用 Python 编写的软件,会定期更新。最好的方法是什么?所有机器都将安装 Windows 7。
另外,请原谅我问题的含糊之处。这将是我第一次必须实施更新程序。请随意提及您希望我补充的细节。
What is the best method to push changes to a program written in Python? I have a piece of software that is written in Python that will regularly be updated. What would be the best way to do this? All the machines will have Windows 7.
Also, excuse the ambiguity of my question. This will be my first time having to implement an updating procedure. Feel free to mention specifics you would like me ot add.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您尚未使用 InnoSetup 打包您的程序,我强烈建议您切换到它,因为它有使这类事情变得更容易的设施。您可以在 InnoSetup 脚本中指定任何特殊情况,例如如果文件已经存在则不应更新(即如果您有任何内部配置文件或类似内容)。
接下来,为了让客户端计算机能够找到应用程序的新版本,请在公共 Web 服务器上保留一个非常小的文件,其中包含当前版本的版本号以及最新版本安装程序 exe 的 URL。为了使此文件有用,每当您发布程序的较新版本时,您都必须更新此文件以及 InnoSetup 脚本中的版本号,以及程序中的某种 APP_VERSION 常量。
然后,您需要自己处理更新程序的以下部分:
更新脚本将等待原始进程完全终止(最简单的方法是将原始进程的 PID 作为命令行参数传递,并让更新脚本 每秒左右向该进程发送一个查询信号 0 直到它消失然后,它可以在后台静默运行安装程序,也许同时向用户显示“请稍候...”对话框。一旦安装程序完成并在返回代码中报告成功,更新程序就可以重新启动您的程序。
根据您的应用程序有多大,这比使用 git 或其他 SCM 的方法更浪费带宽。使用这种方法的每次更新都需要下载最新版本应用程序的整个安装程序,而 SCM 只会下载已更改的文件。然而,它的优点是除了常规的Web服务器之外不需要特殊的服务器设施,并且不需要在用户计算机上专门安装SCM客户端。
另外,InnoSetup 总体来说很酷。 :-)
If you're not already packaging your program with InnoSetup, I strongly recommend you switch to it, because it has facilities to make this sort of thing easier. You can specify any special situations, such as files that should not be updated if they already exist (i.e. if you have any internal configuration files or things like that), in the InnoSetup script.
Next, to allow the client machine to find out about new versions of your app, keep a very small file on your public web server that has the version number of the current release and the URL to the latest version's installer exe. For this file to be useful, whenever you release a newer version of your program you must update this file, as well as the version number in the InnoSetup script, and also some kind of APP_VERSION constant in your program.
Then, you'll need to handle these parts of the updater yourself:
The update script will wait for the original process to die completely (easiest way to do this is to pass in the original process's PID as a command line argument and have the update script send a query signal 0 to that process every second or so until it goes away.) It can then run the installer silently in the background, perhaps while displaying a "Please Wait..." dialog to the user. Once the installer is done and reports success in its return code, the updater can restart your program.
Depending on how big your app is, this is more wasteful of bandwidth than the method using git or another SCM. Every update with this approach would involve downloading the entire installer for the latest version of the app, whereas an SCM would only download the files that have changed. However, it has the advantage that it requires no special server facilities except a regular web server, and no special installation of the SCM client on the user's computer.
Plus, InnoSetup is just generally cool. :-)
我建议使用源代码控制程序,例如 git 或 subversion。另外,如果您同意每个人都看到代码,您可以将代码发布到 github 上,任何人都可以从中提取代码。你可以将其设置为私有,但你必须为此付费,并且所有用户还必须创建一个 github 帐户并使用他们的 git 安装进行设置。
如果您使用源代码控制程序,其他人将必须通过运行命令来手动进行编辑,但您可以创建一个脚本 pr 批处理文件来执行此操作,并让它在启动时或定期运行。
需要明确的是,如果你想这样做,你必须将代码放在支持 SSH 的服务器上并设置 git。如果您不想完成所有服务器设置,我会推荐 github。
git- http://git-scm.com/ (对于 Windows 版本,请转到下载并选择 msysGit)
github - https://github.com/
I would suggest using a source control program such as git or subversion. Also, if you are okay with everyone seeing the code, you can post the code on github, where anyone can pull from it. You could make it private, but you would have to pay for it and all the users would also have create a github account and set it up with their git install.
If you use a source control program, the other people will have to pull the edits manually by running a command, but you could make a script pr batch file that does this and have it run at start up or at regular intervals.
Just to be clear, if you want to do this, you will have to put the code on a server with and SSH support and set up git. If you don't want to go through all of the server set up, I would reccomend github.
git- http://git-scm.com/ (For windows version, go to downloads and select msysGit)
github - https://github.com/
对于那些想要研究一些不太过时的东西的人来说,我只是在研究如何创建可以远程更新的 python 应用程序(尽管不限于像 OP 这样的 Windows)。
看起来 esky 已经是一段时间的解决方案了。尽管它自 2018 年以来已被弃用。
最新的解决方案似乎是 pyinstaller 的组合和 pyupdater。请注意,我没有个人经验,我正在寻找朋友。
它似乎支持 windows、linux 和 Mac,而且还支持 python 2 和 3,所以绝对值得一看。
For those of you that would be looking into something a little less dated, I was just looking at how to create python applications that can be updated remotely (though not limited to Windows like OP).
It seems like esky as been a solution for a while. Though it's been deprecated since 2018.
The latest and most up to date solution seem to be a combination of pyinstaller and pyupdater. Note that I don't have personal experience with it, I'm looking for a friend.
It seems to support windows, linux and Mac though and both python 2 and 3 so definitely worth having a look.
DSimon 的回答很好地描述了应用程序更新的基本原理。
但是,更新安全性完全是另一回事:您不希望您的客户端最终安装恶意文件。
PyUpdater,如 jlengrand 的回答,确实提供了一些安全更新功能,但是,不幸的是,PyUpdater 4.0 已损坏 并且已经半年多没有新版本发布了(现在是 2022 年 8 月)。
还有 python-tuf,它是 更新框架 (TUF)。
TUF (
python-tuf
) 尽一切努力确保您的更新文件安全分发。但是,它不处理特定于应用程序的事情,例如检查新应用程序版本和客户端安装。2023 年更新:PyUpdater 不再维护。该项目存档于 2022 年 9 月 26 日。为了提供替代方案,我们在
python-tuf
处理特定于应用程序的部分。 PyUpdater 的替代品现已作为tufup
提供。The basic principles of application updates are described well by DSimon's answer.
However, update security is a different matter altogether: You don't want your clients to end up installing malicious files.
PyUpdater, as suggested in jlengrand's answer, does provide some secure update functionality, but, unfortunately, PyUpdater 4.0 is broken and there has not been a new release in over half a year (now Aug 2022).
There's also python-tuf, which is the reference implementation of The Update Framework (TUF).
TUF (
python-tuf
) does everything humanly possible to ensure your update files are distributed securely. However, it does not handle application-specific things like checking for new application versions and installation on the client side.Update 2023: PyUpdater is no longer maintained. The project was archived on Sep 26, 2022. To provide an alternative, we created a layer on top of
python-tuf
to handle the application-specific parts. This replacement for PyUpdater is now available astufup
.