我正在使用 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/