开发者

md5 module error

开发者 https://www.devze.com 2022-12-16 13:20 出处:网络
I\'m using an older version of PLY that uses the md5 module (among others): import re, types, sys, cStringIO, md5, os.path

I'm using an older version of PLY that uses the md5 module (among others):

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

... although the script runs but not without this error:

DeprecationWarning: the md5 module is de开发者_如何学Cprecated; use hashlib instead

How do I fix it so the error goes away?

Thanks


I think the warning message is quite straightforward. You need to:

from hashlib import md5

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


That's not an error, that's a warning.

If you still insist on getting rid of it then modify the code so that it uses hashlib instead.


As mentioned, the warning can be silenced. And hashlib.md5(my_string) should do the same as 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

As @Dyno Fu says: you may need to track down what your code actually calls from md5.


please see the docs here , 28.5.3 gives you a way to suppress deprecate warnings. Or on the command line when you run your script, issue -W ignore::DeprecationWarning


i think the warning is ok,still you can use the md5 module,or else hashlib module contains md5 class

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

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


What about something like this?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号