如何使用 python-apt API 安装软件包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17537390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:28:08  来源:igfitidea点击:

How to install a package using the python-apt API

pythonapt

提问by AlejandroVK

I'm quite a newbie when it comes to Python, thus I beg foregiveness beforehand :). That said, I'm trying to make a script that, among other things, installs some Linux packages. First I tried to use subopen as explained here. While this can eventually work, I stumbled upon the python-apt APIand since I'm not a big fan or re-inventing the wheel, I decided to give a try.

说到 Python,我是个新手,因此我事先请求原谅 :)。也就是说,我正在尝试制作一个脚本,其中包括安装一些 Linux 软件包。首先,我尝试使用 subopen 作为解释here。虽然这最终可以奏效,但我偶然发现了python-apt API并且由于我不是一个忠实的粉丝或重新发明轮子,我决定尝试一下。

Problem comes when trying to find examples/tutorials on installing a package using python-apt. Searching the documentation I found the PackageManagerclass that has some methods to install a package. I tried some simple code to get this working:

尝试查找有关使用 python-apt 安装软件包的示例/教程时出现问题。搜索文档我发现PackageManager类有一些安装包的方法。我尝试了一些简单的代码来让它工作:

apt_pkg.PackageManager.install("python")

This does not seem to work that easily, the install method expects apt_pkg.PackageManager instead of a plain String. Thus, looking a bit more, I found this examplethat looks promising, but I'm a bit reluctant to use since I don't really understand some of what is happening there.

这似乎并不容易,安装方法需要 apt_pkg.PackageManager 而不是纯字符串。因此,多看一点,我发现这个例子看起来很有希望,但我有点不愿意使用,因为我并不真正了解那里发生的一些事情。

Then, has anyone tried to install a package using python-apt or should I go for using plain-old subopen style?

那么,有没有人尝试使用 python-apt 安装软件包,还是应该使用普通的 subopen 样式?

Thanks!

谢谢!

采纳答案by Austin Phillips

It's recommended to use the aptmodule from the python-aptDebian package. This is a higher level wrapper around the underlying C/C++ libapt-xxxlibraries and has a Pythonic interface.

建议使用Debian 软件包中的apt模块python-apt。这是围绕底层 C/C++libapt-xxx库的更高级别的包装器,并具有 Pythonic 接口。

Here's an example script which will install the libjs-yui-docpackage:

这是一个将安装libjs-yui-doc软件包的示例脚本:

#!/usr/bin/env python
# aptinstall.py

import apt
import sys

pkg_name = "libjs-yui-doc"

cache = apt.cache.Cache()
cache.update()
cache.open()

pkg = cache[pkg_name]
if pkg.is_installed:
    print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
    pkg.mark_install()

    try:
        cache.commit()
    except Exception, arg:
        print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))

As with the use of apt-get, this must be run with superuser privileges to access and modify the APT cache.

与使用 一样apt-get,它必须以超级用户权限运行才能访问和修改 APT 缓存。

$ sudo ./aptinstall.py

If you're attempting a package install as part of a larger script, it's probably a good idea to only raise to root privileges for the minimal time required.

如果您尝试将软件包安装作为较大脚本的一部分,那么仅在所需的最短时间内提升到 root 权限可能是个好主意。

You can find a small example in the /usr/share/pyshared/apt/progress/gtk2.py:_test()function showing how to install a package using a GTK front-end.

您可以在/usr/share/pyshared/apt/progress/gtk2.py:_test()显示如何使用 GTK 前端安装包的函数中找到一个小示例。