0

by using python apt, i can install packages. Before and after installing the package, i updated the cache. However, before and after installing the package, is_installed returns the same, in other words, it gives wrong result.

After installing the package, i need to check whether the package is installed or not. Here is my code :

import apt.cache

def cache_update():
    cache = apt.cache.Cache()    
    cache.update()
    pkg = cache["p7zip-full"]
    print pkg.is_installed  # prints false

    if pkg.is_installed:
       print "it is already installed. Invalid request! "
       pkg.mark_delete()
    else:
       print "it is not installed.Now you are installing..."
       pkg.mark_install()

    cache.commit()


    print "DONE."
    cache.update()  
    print pkg.is_installed # prints false.



if __name__ == '__main__':
    cache_update()
1
  • why using pkg.mark_delete() when package is installed? Jun 1, 2015 at 23:07

2 Answers 2

1

For some reason, python doesn't carry over the variable after the cache.commit(). If you define it again afterwards, it returns the right pkg.is_installed answer.

#!/usr/bin/python

import apt.cache
pack1 = 'p7zip-full'

def cache_update():
    cache = apt.cache.Cache()    
    cache.update()
    pkg = cache[pack1]
    print pkg.is_installed  # prints false

    if pkg.is_installed:
       print "it is already installed. Invalid request! "
       pkg.mark_delete()
    else:
       print "it is not installed.Now you are installing..."
       pkg.mark_install()

    cache.commit()

    print "DONE."
    cache = apt.cache.Cache()
    pkg = cache[pack1]
    print pkg.is_installed # prints true.


if __name__ == '__main__':
    cache_update()
1
  • Is there a way to track the progress of the install ? After executing cache.commit, I don't know how to track the progress if there's multiple installs or removes. Might you have an idea? Jan 22, 2017 at 2:42
1

You can re-init with open() like so:

cache.commit()
cache.open()
print cache[pack1].is_installed

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .