python 2.6 reports that the md5 module is obsolete and hashlib should be used. If I change import md5
to import hashlib
I will solve for python 2.5 and python 2.6, but not for python 2.4, which has no hashlib module (leading to a ImportError, which I can catch).
Now, to fix it, I could do a try/catch, and define a getMd5() function so that a proper one gets defined according to the result of the try block. Is this solution ok?
How would you solve this issue in a more general case, like, for example: you have two different libraries with the same objective but different interface, and you want to use one, but fall back and use the other if the first one is not found.
In general the following construct is just fine:
try:
import module
except ImportError:
# Do something else.
In your particular case, perhaps:
try:
from hashlib import md5
except ImportError:
from md5 import md5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With