使用 apt 模块更新 python 本身
我正在编写一个 python 脚本,该脚本将在 EC2 计算机上作为 user-data- 运行脚本。我正在尝试弄清楚如何升级机器上的软件包,类似于 bash 命令:
$ sudo apt-get -qqy update && sudo apt-get -qqy upgrade
我知道我可以使用 python 中的 apt 软件包来执行此操作:
import apt
cache=apt.Cache()
cache.update()
cache.open(None)
cache.upgrade()
cache.commit()
问题是如果 python 本身会发生什么是升级的软件包之一。有没有办法在升级后重新加载解释器和脚本并从中断处继续?
现在我唯一的选择是使用 shell 脚本作为我的用户数据脚本,其唯一目的是升级软件包(可能包括 python),然后将其余代码放入 python 中。我想消除使用 shell 脚本的额外步骤。
I am writing a python script that will run on an EC2 machine as the user-data-script. I'm trying to figure out how I can upgrade the packages on the machine similar to the bash command:
$ sudo apt-get -qqy update && sudo apt-get -qqy upgrade
I know I can use the apt
package in python to do this:
import apt
cache=apt.Cache()
cache.update()
cache.open(None)
cache.upgrade()
cache.commit()
Problem is what happens if python itself was one of the packages that was upgraded. Is there a way to reload the interpreter and script following this upgrade and continue where it left off?
Right now my only choice is to use a shell script as my user-data script for the sole purpose of upgrading packages (including possibly python) and then dropping into python for the rest of my code. I'd like to eliminate the extra step of using a shell script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我已经明白了:
I think I figured it out:
使用链接。
在 Python 应用程序中,您可以编写一个 shell 脚本来执行您需要的操作。在本例中,该 shell 脚本将升级 Python。由于它是在Python关闭后运行的,所以不存在冲突。事实上,
next.sh
可以启动同一个(或另一个)Python 应用程序。如果您交替使用两个 shell 脚本first.sh
和next.sh
,您可以根据需要将任意数量的这些调用链接在一起。Use chaining.
Inside the Python app, you would write out a shell script to do what you need. In this case, that shell script would upgrade Python. Since it runs after Python has shut down, there is no conflict. In fact,
next.sh
could start up the same (or another) Python app. If you alternate between two shell scriptsfirst.sh
andnext.sh
you could chain as many of these invocations together as you want.