检查我的Python是否具有所有必需的软件包 [英] Check if my Python has all required packages

查看:90
本文介绍了检查我的Python是否具有所有必需的软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个requirements.txt文件,其中包含虚拟环境所需的软件包列表.是否有可能找出文件中提到的所有软件包是否都存在.如果缺少某些软件包,如何找出丢失的软件包?

I have a requirements.txt file with a list of packages that are required for my virtual environment. Is it possible to find out whether all the packages mentioned in the file are present. If some packages are missing, how to find out which are the missing packages?

推荐答案

更新:

通过distutils.text_file.TextFile可以实现此目的的最新改进方法.有关详细信息,请参见下面的Acumenus的答案.

An up-to-date and improved way to do this is via distutils.text_file.TextFile. See Acumenus' answer below for details.

原始:

做到这一点的Python方法是通过pkg_resources API .需求以setuptools可以理解的格式编写.例如:

The pythonic way of doing it is via the pkg_resources API. The requirements are written in a format understood by setuptools. E.g:

Werkzeug>=0.6.1
Flask
Django>=1.3

示例代码:

import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict

# dependencies can be any iterable with strings, 
# e.g. file line-by-line iterator
dependencies = [
  'Werkzeug>=0.6.1',
  'Flask>=0.9',
]

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is thrown. 
pkg_resources.require(dependencies)

这篇关于检查我的Python是否具有所有必需的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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