msamogh
msamogh

Reputation: 147

Supporting multiple versions of a Python dependency

I'm the author of an open-source library (N) that provides an enhancement to a popular Python library (P). Recently, P released a new version where they changed some code that affects my library N.

I have a simple fix for N to make it compatible with the new version of P, but I want to know if there's a good way of supporting both versions of P in my library without resorting to an if-else-ing around the different versions. Going forward, I want to support both versions of library P, so just moving to the new one is not an option.

Upvotes: 2

Views: 1814

Answers (1)

Gabriel Cappelli
Gabriel Cappelli

Reputation: 4170

Many packages have a __version__ attribute.

You can use modules/classes/subclassing to elegantly support multiple versions, but you`ll probably still need one if-else to load the correct implementation.

import marshmallow        
from packaging import version

if version.parse(marshmallow.__version__) >= version.parse("3.0.0"):
    import new_implementation as impl
else:
    import old_implementation as impl

impl.foo()

Upvotes: 1

Related Questions