md5模块错误

5 投票
6 回答
14106 浏览
提问于 2025-04-15 18:08

我正在使用一个旧版本的PLY,它使用了md5模块(还有其他模块):

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

... 虽然这个脚本可以运行,但还是会出现这个错误:

DeprecationWarning: the md5 module is deprecated; use hashlib instead

我该怎么做才能让这个错误消失呢?

谢谢

6 个回答

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 调用了什么。

2

这不是错误,而是一个警告。

如果你还是想去掉这个警告,那就把代码改成使用 hashlib 这个库。

10

我觉得这个警告信息很简单明了。你需要:

from hashlib import md5

或者你可以使用 python 版本低于 2.5,详细信息可以查看这个链接:http://docs.python.org/library/md5.html

撰写回答