代码之家  ›  专栏  ›  技术社区  ›  eozzy

MD5模块错误

  •  5
  • eozzy  · 技术社区  · 15 年前

    我使用的是旧版本的PLY,它使用MD5模块(以及其他模块):

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

    …虽然脚本运行但没有此错误:

    DeprecationWarning: the md5 module is deprecated; use hashlib instead
    

    如何修复它以消除错误?

    谢谢

    6 回复  |  直到 6 年前
        1
  •  9
  •   gsamaras a Data Head    6 年前

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

    from hashlib import md5
    

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

        2
  •  2
  •   Ignacio Vazquez-Abrams    15 年前

    这不是错误,这是警告。

    如果您仍然坚持要去掉它,那么修改代码以便它使用 hashlib 相反。

        3
  •  2
  •   telliott99    15 年前

    如前所述,警告可以静音。和hashlib.md5(my_字符串)应该与md5.md5(my_字符串)相同。

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

        4
  •  0
  •   ghostdog74    15 年前

    请看文件 here ,28.5.3提供了一种抑制拒绝警告的方法。或者在命令行上运行脚本时,发出 -W ignore::DeprecationWarning

        5
  •  0
  •   appusajeev    15 年前

    我认为警告是正常的,您仍然可以使用MD5模块,否则hashlib模块包含MD5类

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

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

        6
  •  0
  •   SomerandomGuy    10 年前

    像这样的东西怎么样?

    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"))
    
    推荐文章