2016-12-12 5 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; print (ccc .__ version __)'가 작동합니다 –

+0

요점은 pkg_resources.require가 제공 한 결과로 작업하고 싶다는 것입니다. ccc에만 잘못된 버전이 있음을 보여주는 결과가 있어야합니다. – AndiGasman

+0

그런 다음 해당 객체를 변수에 할당해야합니다. 지금 당신이 그것을 인쇄 한 후에 그냥 던지고 있습니다. –

답변

0

입니다. exeception을 변수에 할당하고 그 변수로 작업해야합니다. 코드는 다음과 같습니다.

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()