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.
To pip install more than one Python package, the packages can be listed in line with the same pip install command as long as they are separated with spaces. Here we are installing both scikit-learn and the statsmodel package in one line of code. You can also upgrade multiple packages in one line of code.
Ensure you can run pip from the command lineRun python get-pip.py . 2 This will install or upgrade pip. Additionally, it will install setuptools and wheel if they're not installed already. Be cautious if you're using a Python install that's managed by your operating system or another package manager.
How to Manually Download Packages from PyPI. You can download packages directly from PyPI by doing the following: Point your browser at https://pypi.org/project/<packagename> Select either Download Files to download the current package version, or Release History to select the version of your choice.
Try checking the return value for non-zero, which indicates an error occurred with the install. Not all errors trigger exceptions.
import pip
def install(package_name):
try:
pipcode = pip.main(['install', package_name])
if pipcode != 0:
print("Unable to install " + package_name + " ; pipcode %d" % pipcode)
except:
print("Unable to install " + package_name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With