AndiGasman
AndiGasman

Reputation: 123

Print information from exception in pkg_resources

I'd like to check if all required modules are installed in the correct version using pkg_resources.require. It all works fine, but I don't know how to print out the information if pkg_resources raises a pkg_resource.VersionConflict.

This example will raise an exception because the installed version of ccc is 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()

Upvotes: 1

Views: 253

Answers (1)

AndiGasman
AndiGasman

Reputation: 123

got it. I have to assign the exeception to a variable and work with that. Here's the code:

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

Upvotes: 1

Related Questions