0
如何使用Python/python-apt以編程方式獲得與「dpkg --get-selections」相同的結果?如何以編程方式從apt獲取軟件包列表?
如何使用Python/python-apt以編程方式獲得與「dpkg --get-selections」相同的結果?如何以編程方式從apt獲取軟件包列表?
要顯示的前五個包:
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>
最好不要它循環的範圍(0,5)並調用cache.Packages.next()? – elnabo
這對我不起作用。我得到了:「AttributeError:'apt_pkg.PackageList'對象沒有'next'屬性。 dir()表示該對象上沒有暴露成員。 –