Fix UnboundLocalError when trying to reinstall or remove a package