Home > Software engineering >  How to get list of all packages installed by apt?
How to get list of all packages installed by apt?

Time:12-19

I would like to have all the package names installed by apt (or by anything not pip) as a "Python list".

This bash command writes all the packages into the file apt_list.txt:

dpkg -l | grep ^ii | awk '{print $2}' > apt_list.txt

I consider reading this file by Python code to have the list, but this solution looks inefficient to me:

import os
os.system("dpkg -l | grep ^ii | awk '{print $2}' > apt_list.txt")
# ...
# Python code
# to read the file apt_list.txt
# ...

So what is the direct and efficient way to do that?

Another solution that comes to my mind is to get the contents (the file list) of the folders, which include the packages listed by "dpkg -l *" command, directly by Python code. But I suppose these packages maybe in multiple folders and I don't know what these multiple locations are.

CodePudding user response:

if you use subprocess then you can read stdout, so you don't need to write the file

import subprocess

ret=subprocess.run("dpkg -l | grep ^ii | awk '{print $2}'", capture_output=True, shell=True)
my_list=ret.stdout.decode().split('\n')
  • Related