2016-12-12 42 views
0

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

此示例将引发异常,因为安装的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() 
+0

取决于包。有时候'import ccc;打印(ccc .__版本__)'会工作 –

+0

问题是我想使用pkg_resources.require返回的结果。结果中必须有某些内容显示只有ccc具有不正确的版本。 – AndiGasman

+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 as version_error: 
    print("The following modules caused an error:") 
    print("Version installed :", version_error.dist) 
    print("Version required :", version_error.req) 
    exit()