cache = apt.cache.Cache()
cache.update()
pkg = cache[pkg_name]
if pkg.is_installed:
    print('{pkg_name} already installed'.format(pkg_name=pkg_name))
else:
    pkg.mark_install()
try:
    cache.commit()
except Exception as arg:
    pass
How to install a package using the python-apt API