python md5 模块错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2084407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
md5 module error
提问by eozzy
I'm using an older version of PLY that uses the md5 module (among others):
我使用的是旧版本的 PLY,它使用 md5 模块(以及其他):
import re, types, sys, cStringIO, md5, os.path
... although the script runs but not without this error:
...虽然脚本运行但并非没有此错误:
DeprecationWarning: the md5 module is deprecated; use hashlib instead
How do I fix it so the error goes away?
如何修复它以便错误消失?
Thanks
谢谢
回答by Dyno Fu
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
或者你可以使用 python < 2.5,http://docs.python.org/library/md5.html
回答by telliott99
As mentioned, the warning can be silenced. And hashlib.md5(my_string) should do the same as md5.md5(my_string).
如前所述,警告可以被静音。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
As @Dyno Fu says: you may need to track down what your code actually calls from md5.
正如@Dyno Fu 所说:您可能需要追踪您的代码实际从 md5 调用的内容。
回答by Ignacio Vazquez-Abrams
回答by ghostdog74
回答by appusajeev
i think the warning is ok,still you can use the md5 module,or else hashlib module contains md5 class
我认为警告没问题,您仍然可以使用 md5 模块,否则 hashlib 模块包含 md5 类
import hashlib
a=hashlib.md5("foo")
print a.hexdigest()
this would print the md5 checksum of the string "foo"
这将打印字符串“foo”的 md5 校验和
回答by SomerandomGuy
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"))