如何使用 python-apt API 安装包 [英] How to install a package using the python-apt API

查看:38
本文介绍了如何使用 python-apt API 安装包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 API and since I'm not a big fan or re-inventing the wheel, I decided to give a try.

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

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

apt_pkg.PackageManager.install("python")

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

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 example that looks promising, but I'm a bit reluctant to use since I don't really understand some of what is happening there.

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

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

谢谢!

推荐答案

推荐使用 python-apt Debian 软件包中的 apt 模块.这是底层 C/C++ libapt-xxx 库的高级包装器,并具有 Pythonic 接口.

It's recommended to use the apt module from the python-apt Debian package. This is a higher level wrapper around the underlying C/C++ libapt-xxx libraries and has a Pythonic interface.

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

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

#!/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))

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

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

$ sudo ./aptinstall.py

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

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.

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

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.

这篇关于如何使用 python-apt API 安装包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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