DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5的解决办法

今天在学习python的md5模块的时候,做练习,遇到DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5的警告;

# /usr/bin/python
# -*- coding:utf-8 -*-

import md5
hash = md5.new()
hash.update('spam,spam,and egges')
print repr(hash.digest())

执行结果为:

解决办法:

# /usr/bin/python
# -*- coding:utf-8 -*-
try:
    import hashlib
    hash = hashlib.md5()
except ImportError:
    # for Python << 2.5
    import md5
    hash = md5.new()
hash.update('spam,spam,and egges')
print repr(hash.digest())

如代码所示,我使用的python版本是2.6.6,所以会有警告,如果是2.5之前的版本就不会有了。

 

posted on 2013-02-20 16:09  mingaixin  阅读(5138)  评论(0编辑  收藏  举报