![如何查找已安装的多个软件包?](https://linux22.com/image/1190360/%E5%A6%82%E4%BD%95%E6%9F%A5%E6%89%BE%E5%B7%B2%E5%AE%89%E8%A3%85%E7%9A%84%E5%A4%9A%E4%B8%AA%E8%BD%AF%E4%BB%B6%E5%8C%85%EF%BC%9F.png)
我刚刚发现我已经安装了两者gcc-10
(gcc-11
并且已经安装了apt purge gcc-10 && apt autoremove
)。
我想知道我可能还安装了哪些多个版本的其他软件包?
PS. 似乎gcc-12
也存在,但apt install gcc-12 && apt purge gcc-11
由于unmet dependencies
)而失败
答案1
以下是我最终使用的方法:
import subprocess as sp
versioned = {}
with sp.Popen(["apt","list","--installed"], encoding="ascii",
stdout=sp.PIPE, stderr=sp.STDOUT) as pipe:
for pack in pipe.stdout:
if "/" not in pack:
print(pack)
continue
name = pack.split("/",1)[0]
name_ver = name.rsplit("-",1)
if len(name_ver) == 2 and name_ver[1].isdigit():
versioned.setdefault(name_ver[0],set()).add(name_ver[1])
print(f"found {len(versioned):,d} versioned packages")
versioned = {n:v for n,v in versioned.items() if len(v) > 1}
print(f"found {len(versioned):,d} packages with multiple versions:\n{versioned}")
打印
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Listing...
found 240 versioned packages
found 3 packages with multiple versions:
{'cpp': {'12', '11'}, 'g++': {'12', '11'}, 'gcc': {'12', '11'}}