2

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.

3
  • use a virtual environment with the versions that work with your library..same with the new one Commented Aug 24, 2019 at 0:54
  • How will virtual environments help me here? I want people to be able to either dynamically install a package depending on their installed P version or failing that, be able to elegantly support multiple versions in my code. Commented Aug 24, 2019 at 8:25
  • then you'll have to provide both versions and check which dependencies are installed before building your package Commented Aug 24, 2019 at 15:55

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.