Md5 module error

I am using an older version of PLY which uses the md5 module (among others):

import re, types, sys, cStringIO, md5, os.path

... although the script works, but not without this error:

DeprecationWarning: the md5 module is deprecated; use hashlib instead

How to fix it so that the error disappears?

thanks

+5
source share
6 answers

I think the warning message is pretty simple. you need


from hashlib import md5

or you can use python <2.5, http://docs.python.org/library/md5.html

+8
source

This is not a mistake, this is a warning.

If you still insist on getting rid of it, change the code to use instead hashlib.

+2
source

, . hashlib.md5 (my_string) , md5.md5 (my_string).

>>> import md5
__main__:1: DeprecationWarning: the md5 module is deprecated; use hashlib instead
>>> import hashlib
>>> s = 'abc'
>>> m = md5.new(s)
>>> print s, m.hexdigest()
abc 900150983cd24fb0d6963f7d28e17f72
>>> m = hashlib.md5(s)
>>> print s, m.hexdigest()
abc 900150983cd24fb0d6963f7d28e17f72
>>> md5(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> md5.md5(s)
<md5 HASH object @ 0x100493260>
>>> m = md5.md5(s)
>>> print s, m.hexdigest()
abc 900150983cd24fb0d6963f7d28e17f72

@Dyno Fu: , md5.

+2

see docs here , 28.5.3 gives you a way to suppress tireless warnings. Or on the command line when running the script, release-W ignore::DeprecationWarning

0
source

I think the warning is ok, still you can use the md5 module, otherwise the hashlib module contains the md5 class

import hashlib
a=hashlib.md5("foo")
print a.hexdigest()

this will print the md5 checksum of the string "foo"

0
source

How about something like that?

try:
    import warnings
    warnings.catch_warnings()
    warnings.simplefilter("ignore")
    import md5
except ImportError as imp_err:
    raise type(imp_err), type(imp_err)("{0}{1}".format(
        imp_err.message,"Custom import message"))
0
source

All Articles