【问题标题】:Print information from exception in pkg_resources从 pkg_resources 中的异常打印信息
【发布时间】:2016-12-12 16:43:24
【问题描述】:

我想使用 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()

【问题讨论】:

  • 取决于包。有时import ccc; print(ccc.__version__) 会起作用
  • 重点是我想处理 pkg_resources.require 反馈的结果。结果中一定有东西表明只有 ccc 的版本不正确。
  • 那么您需要将该对象分配给一个变量。现在你只是在打印后扔掉它。

标签: python-3.x pkg-resources


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 2011-07-14
    • 2015-09-29
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多