I am trying to install a list of packages using pip.
The code which I used is:
import pip
def install(package_name):
try:
pip.main(['install', package_name])
except:
print("Unable to install " + package_name)
This code works fine and if a package is not available, it gives an error:
No matching distributions found
However, what I am trying to do is if an installation fails (for eg: invalid package name), I want to print the package which failed.
What can be done for that?
Any help would be appreciated, thank you.
You can check the value of package to verify if no matching distribution was find. Normally the package will return 0 if exists a installation candidate, otherwise will return 1 for no candidate found
So, if you try to do something like this:
Will return:
Because there's no valid package for "Birtualenvs". But with a valid package:
Will return:
Try checking the return value for non-zero, which indicates an error occurred with the install. Not all errors trigger exceptions.