How to access a different version of a python library listed as a dependency

I know very well that python virtual environments do not allow installation of multiple versions of packages.

Still, I am facing a case where a testing I am developing needs to be able to call a function from a newer version of one of its dependencies, that happens in a single place so it should be possible to find a way to contain it.

Does anyone have a working example where something like this was used?

I am wondering if it temporary altering the sys.path before doing the import would work or if it would create other issues.

Almost certainly it would create other issues.

I think the best option if it’s feasible is to simply copy and paste the version of the function you need into your code (particularly if it’s just one function). A second best version of this is to depend on the new version and vendor the old version under a different namespace in your application, so like myapp._vendored.blah2_3.

Obviously neither of these is ideal, but any “official” solution that works in general would likely need to be monstrously complex, so I think that this sort of thing is best handled with hacks and workarounds while you update your application to support a single version of your dependencies.

1 Like