5

我正在使用使用 md5 模块(以及其他)的旧版本 PLY:

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

...虽然脚本运行但并非没有此错误:

DeprecationWarning: the md5 module is deprecated; use hashlib instead

如何修复它以使错误消失?

谢谢

4

6 回答 6

10

我认为警告信息非常简单。你需要:

from hashlib import md5

或者您可以使用 python < 2.5,http://docs.python.org/library/md5.html

于 2010-01-18T07:17:22.867 回答
2

这不是错误,这是警告。

如果您仍然坚持要摆脱它,请修改代码以便它使用它hashlib

于 2010-01-18T07:22:10.090 回答
2

如前所述,可以使警告静音。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 调用的内容。

于 2010-01-18T15:36:30.280 回答
0

请在此处查看文档,28.5.3 为您提供了一种抑制弃用警告的方法。或者在运行脚本时在命令行上,发出-W ignore::DeprecationWarning

于 2010-01-18T07:24:35.640 回答
0

我认为警告没问题,你仍然可以使用 md5 模块,否则 hashlib 模块包含 md5 类

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

这将打印字符串“foo”的 md5 校验和

于 2010-01-18T09:31:12.417 回答
0

这样的事情呢?

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"))
于 2015-04-20T19:49:08.463 回答