Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any Python code to get the list of packages installed on Ubuntu along with the version of the package? I have seen Python code at Generating list of manually installed packages and querying individual packages. But that doesn't have the listing of version ID. Can anyone know how to get the version of the installed packages?

share|improve this question
up vote 2 down vote accepted

I'm not sure if there is existing code for that. But you can look at file /var/lib/dpkg/status. All information about installed packages is stored in this file.

To write code that will parse this file is pretty easy even in case you're not great in python.

share|improve this answer

Not sure if this shouldn't be migrated to askubuntu, but just use the same approach as in the mentioned thread. Run dpkg/dpkg-query to get a dump of all the data and use standard python parsing mechanisms to extract the relevant information.

Of course, since you're already forking, you can do all the text mangling in bash itself.

Alternatively, find the state file that contains versions, if there is any ("Dir::Etc::Status"?). This approach would be the cleanest.

share|improve this answer

Pass the -F (--format) option to aptitude to specify which fields are displayed.

aptitude --disable-columns -F '%p %v' search '~A'

From Python, use the apt_pkg module from the python-apt package.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.