Python-apt : install package with specific version

标签 python apt

我正在使用 python-apt 安装 debian 软件包。我需要能够使用特定版本安装它,但不知道如何安装。根据 candidate 的文档:

Just assign a Version() object, and it will be set as the candidate version.

目前我正在按以下方式安装该软件包:

import apt
cache = apt.cache.Cache()
pkg = cache['byobu']  # Or any random package for testing
pkg.mark_install()
cache.commit()

到目前为止,我发现设置版本的唯一方法是通过像这样的 apt.apt_pkg,但我不知道如何从这里开始:

pkg_name = 'byobu'
cache = apt.cache.Cache()
pkg = cache[pkg_name]
version = apt.apt_pkg.Cache()[pkg_name].version_list[1]  # 5.77-0ubuntu1
new_version = apt.package.Version(pkg, version)  # 5.77-0ubuntu1
new_version.package.candidate  # 5.77-0ubuntu1.2 <---
new_version.package.mark_install()
cache.commit()  # Returns True

最后的版本是已安装的版本,cache.commit() 只是返回 True 而不执行任何操作(可能是因为候选版本是已安装的版本)。我做错了什么?

最佳答案

在以结构化方式写下此内容后,我终于明白 pgk.candidate 可以被我的 new_version 覆盖。我之前尝试过,但没有混合使用 apt.package.Version、apt.cache.Cache 和 apt.apt_pkg.Cache。

我把它留在这里供其他人将来使用。最终示例代码:

pkg_name = 'byobu'
cache = apt.cache.Cache()
package = cache[pkg_name]
version = apt.apt_pkg.Cache()[pkg_name].version_list[1]
candidate = apt.package.Version(package, version)
package.candidate = candidate
package.mark_install()
cache.commit()

编辑:

感觉很愚蠢,意识到我不必构建版本,而只需从版本列表中使用它...记住 children ,不要在喝醉时编码。

更好的最终代码:

cache = apt.cache.Cache()
package = cache[package_name]
candidate = package.versions.get(version)
package.candidate = candidate
package.mark_install()
cache.commit()

关于Python-apt : install package with specific version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162051/

相关文章:

python - Unresolved 导入 :reactor in PyDev with twisted module

python - 使用 xslwriter 在图中创建点

linux - 在 x64 Ubuntu 主机上获取 arm 的开发库包?

regex - 为什么 apt-cache search 会找到与给定正则表达式不匹配的包?

python-2.7 - 如何在没有root的情况下安装python-dev?

python - 在python中从ElasticSearch索引中删除文档

python - TensorFlow 优化器是否最小化 API 实现的小批量?

python - 从 txt 文件中提取连续的文本部分

python - 文件位置和用法 : apt-get install python-<pkg> vs. pip3 install <pkg>

linux - 如何使用 YUM 或 Apt 包管理器在 Linux 中创建安装程序包?