如何以编程方式从apt获取包列表?

时间:2013-07-09 06:27:46

标签: python linux package apt dpkg

如何使用Python / python-apt以编程方式获得与“dpkg --get-selections”相同的结果?

1 个答案:

答案 0 :(得分:2)

显示前五个包:

import apt_pkg

apt_pkg.init()
cache = apt_pkg.Cache()

i = 5
for pkg in cache.Packages:
  print(pkg)
  i -= 1
  if i < 0:
    break

输出(在我的Ubuntu系统上运行时):

$ python apt_package_list.py
Reading package lists... Done
Building dependency tree       
Reading state information... Done
<apt_pkg.Package object: name:'tesseract-ocr-epo' section: 'universe/graphics' id:48834>
<apt_pkg.Package object: name:'omninotify' section: '' id:48674>
<apt_pkg.Package object: name:'pipenightdreams' section: 'universe/games' id:43500>
<apt_pkg.Package object: name:'mumudvb' section: 'universe/misc' id:41568>
<apt_pkg.Package object: name:'mpg123-alsa' section: '' id:41448>
<apt_pkg.Package object: name:'tbb-examples' section: 'universe/doc' id:38301>