使用pip从列表安装软件包 [英] Installing packages from a list using pip

查看:117
本文介绍了使用pip从列表安装软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pip安装软件包列表.

I am trying to install a list of packages using pip.

我使用的代码是:

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.

对此可以做什么?

任何帮助将不胜感激,谢谢.

Any help would be appreciated, thank you.

推荐答案

尝试检查返回值是否为非零,这表明安装发生了错误.并非所有错误都会触发异常.

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)

这篇关于使用pip从列表安装软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆