Python : error with importing md5

老子叫甜甜 提交于 2019-12-13 15:13:54

问题


I have problem with importing md5 library I just use the code below:

import md5
filemd5 = md5.new(password.strip()).hexdigest()

I also have tried this code :

from hashlib import md5
filemd5 = md5.new(password.strip()).hexdigest()

also this code :

from md5 import md5

But none of them are working ! when I run the code,it gives me this error:

11.py", line 1, in <module>
import md5
ImportError: No module named 'md5'

What Should I Do ? Am I Importing The Wrong Library ?


回答1:


md5 is not a module, it is an object. But it has no new method. It just has to be built, like any object.

Use as follows:

import hashlib

m=hashlib.md5(bytes("text","ascii"))   # python 3
m=hashlib.md5("text")   # python 2
print(m.hexdigest())

results in:

1cb251ec0d568de6a929b520c4aed8d1

You can also create the object empty and update it afterwards (more than once!)

m=hashlib.md5()
m.update("text")   # python 2
m.update(bytes("text","ascii")) # python 3

note that Python 3 requires an encoded bytes object, not string, because Python 3 makes a difference between string and binary data. MD5 is useful on binary and strings.



来源:https://stackoverflow.com/questions/39423542/python-error-with-importing-md5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!