1

我想使用 pkg_resources.require 检查是否所有必需的模块都安装在正确的版本中。一切正常,但如果 pkg_resources 引发 pkg_resource.VersionConflict,我不知道如何打印信息。

此示例将引发异常,因为安装的 ccc 版本是 1.0.0。

dependencies = [
        'aaa=0.7.1',
        'bbb>=3.6.4',
        'ccc>=2.0.0'
    ]
try:
    print(pkg_resources.require(dependencies))
except pkg_resources.VersionConflict:
    print ("The following modules caused an error:")
    // What do i have to do to print out the currently installed version of ccc and the required version using the returned information from pkg_resourcens//
exit()
4

1 回答 1

1

知道了。我必须将异常分配给一个变量并使用它。这是代码:

dependencies = [
    'aaa=0.7.1',
    'bbb>=3.6.4',
    'ccc>=2.0.0'
]
try:
    print(pkg_resources.require(dependencies))
except pkg_resources.VersionConflict as version_error:
    print("The following modules caused an error:")
    print("Version installed :", version_error.dist)
    print("Version required  :", version_error.req)
    exit()
于 2016-12-12T20:42:43.750 回答