Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing packages from a list using pip

Tags:

python

pip

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.

like image 406
Gokul Avatar asked Aug 21 '17 14:08

Gokul


People also ask

Can you pip install multiple packages at once?

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.

How do we install packages in Python using pip?

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 do I manually Download pip packages?

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.


1 Answers

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)
like image 182
tdube Avatar answered Oct 04 '22 06:10

tdube