关于pip:检查我的Python是否包含所有必需的包

Check if my Python has all required packages

我有一个requirements.txt文件,其中包含我的虚拟环境所需的包列表。是否可以查明文件中提到的所有包是否都存在?如果一些包裹丢失了,如何找出丢失的包裹?


通过pkg_resourcesAPI实现这一点的方法非常简单。这些要求以SETUPTOOLS理解的格式编写。例如:

1
2
3
Werkzeug>=0.6.1
Flask
Django>=1.3

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
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)


您可以运行pip freeze查看已安装的内容,并将其与requirements.txt文件进行比较。

如果您想安装丢失的模块,可以运行pip install -r requirements.txt,然后安装丢失的模块,最后告诉您哪些模块丢失并安装了。


根据Zaur的回答,假设您确实使用了requirements.txt文件,那么您可能需要一个单元测试,可能是在tests/test_requirements.py中,以确认包的可用性。

此方法使用子测试独立地确认每个需求。这对记录所有故障很有用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from pathlib import Path
import unittest

import pkg_resources


class TestRequirements(unittest.TestCase):
    def test_requirements(self):
       """Recursively confirm that requirements are available."""
        # Ref: https://stackoverflow.com/a/45474387/
        requirements = (Path(__file__).parents[1] / 'requirements.in').read_text().strip().split('
'
)
        requirements = [r.strip() for r in requirements]
        requirements = [r for r in sorted(requirements) if r and not r.startswith('#')]
        for requirement in requirements:
            with self.subTest(requirement=requirement):
                pkg_resources.require(requirement)


您可以通过访问系统站点包来创建virtualenv,并测试包(或其他依赖项)是否已安装。这样就不会真正安装软件包(如果您只想检查)。使用virtualenv包装器的一个例子是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ cat requirements.txt
requests
simplejson

$ mkvirtualenv --system-site-packages test
Running virtualenv with interpreter /usr/bin/python2
New python executable in test/bin/python2
Also creating executable in test/bin/python
Installing setuptools, pip...done.

$ pip install -r requirements.txt
Downloading/unpacking requests (from -r requirements.txt (line 1))
  Downloading requests-2.10.0-py2.py3-none-any.whl (506kB): 506kB downloaded
Requirement already satisfied (use --upgrade to upgrade): simplejson in /usr/lib/python2.7/dist-packages (from -r requirements.txt (line 2))
Installing collected packages: requests
Successfully installed requests
Cleaning up...

$ pip install -r requirements.txt
Requirement already satisfied (use --upgrade to upgrade): requests in /home/yucer/.virtualenvs/test/lib/python2.7/site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): simplejson in /usr/lib/python2.7/dist-packages (from -r requirements.txt (line 2))
Cleaning up...

$ deactivate

$ rmvirtualenv test
Removing test...

如果requirements.txt类似于:

1
2
3
4
django
oursql
sys
notexistingmodule

然后,以下脚本将告诉您缺少哪些模块:

1
2
3
4
5
6
7
8
#!/usr/bin/python3
fname = 'requirements.txt'
with open(fname, 'r', encoding='utf-8') as fhd:
    for line in fhd:
        try:
            exec("import" + line)
        except:
            print("[ERROR] Missing module:", line)

这将打印:

1
[ERROR] Missing module: notexistingmodule