以编程方式检查Python依赖是否满足 [英] Programmatically check if Python dependencies are satisfied

查看:642
本文介绍了以编程方式检查Python依赖是否满足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式运行 pip ,并确定当前的virtualenv环境是否符合指定的 requirements.txt 文件。我并不讨厌运行 pip 或任何东西,但我认为,因为它可以读取 requires.txt 类文件,这将是一个好的开始。

I'd like to programmatically run pip and determine whether the current virtualenv environment complies with a specified requirements.txt file. I'm not fussed about running pip or anything, but I thought since it can read requirements.txt-like files, it would be a good start.

但是,我甚至没有找到一种有效运行 pip 从命令行。 pip install -r requirements.txt --no-install 被建议在某处,但它下载每个包,即使这不是一个问题,我不确定如何解释其输出是否满足所有依赖关系。

However, I haven't even found a way of effectively running pip from the command line. pip install -r requirements.txt --no-install was suggested somewhere, but it downloads each package and even if this wasn't a problem, I am unsure of how to interpret its output as to whether or not all dependencies are satisfied.

推荐答案

这篇文章对于获取模块列表有很多好的建议。您可以使用以下代码打印出所有缺少的模块:

This post has a lot of good suggestions for getting a list of modules. You can use the below code to print out all missing modules:

from pkgutil import iter_modules
modules = set(x[1] for x in iter_modules())

with open('requirements.txt', 'rb') as f:
    for line in f:
        requirement = line.rstrip()
        if not requirement in modules:
            print requirement

这篇关于以编程方式检查Python依赖是否满足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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