I recently had the following problem: I’m developing a numerical library in Python (called spuq) that needs scipy at its core. Now one of the functions in scipy, its called btdtri, had a bug for a certain tuple of input parameters. This bug, however, is now fixed in scipy version 0.9 according to the developers of scipy. So in my code I have something like this:
import scipy
def foo(a, b, c):
if scipy.__version__>=(0, 9):
return btdtri(a, b, c)
else:
return my_fixed_btdtri(a, b, c)
This works, however, I don’t really like to litter my code with bug fixes for third party packages. I would rather have that contained in one module, that implements the workaround, and have all the other of my modules uses the patched module automatically.
Now my question is: what would be the best practice to handle cases like this in general? E.g. write my own spuq.contrib.scipy and say there
from scipy import *
if __version__ < (0, 9):
btdtri = my_fixed_btdtri
and instead of importing scipy import spuq.contrib.scipy everywhere? I think that’s complicated and easy to forget (and probably unpythonic and ugly). Maybe there is a way to “hook” automatically into the package loading and modify the scipy module directly so that every other package sees only the patched up package? I think this problem is quite common, so probably there should be some ‘best-practices’ around.
You could “monkey patch” the scipy module. Somewhere in your initialization code, do
Since modules are imported only once, there will be only one module
scipy.special, and all other modules will only see the monkey-patched version.Monkey patching is often seen as useful for testing, but not for production code. In this case, though, I think it is fine since you don’t really change the behaviour of the package — you are fixing a confirmed bug.