我尝试了这段代码:
import hashlib
encrypted = hashlib.sha1(string)
encrypted = encrypted.digest()
但是我收到一个错误,提示“没有名为 hashlib 的模块”。出了什么问题,如何解决?
您可能已经有了 python 版本 < 2.5. Use the
sha
模块。
以下是差异:
>>> import sha
>>> s = sha.new()
>>> s.update('hello')
>>> s.digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'
对
>>> import hashlib
>>> hashlib.sha1('hello').digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'
另外,FWIW 和其他人在这里结束,但对于 hashlib.md5():
import md5
m = md5.new()
...
hashlib 是 python 中的一个新模块/库 2.5 服务器肯定运行 python 2.4 或更早版本
在某些 Python 衍生产品(例如 Jython)上,使用以下命令:
import _hashlib
h = _hashlib()
md5Res = h.openssl_md5("helloYou").hexdigest()
print(md5Res)
查找与未找到的模块相关的此类错误的最简单方法是验证其路径。我完全能够通过控制台运行 python facebook ads api 代码,但是当我通过 c# 尝试此代码时,我遇到了几个与路径相关的错误。
在导入语句之前的给定语句下方给出“hashlib.py 文件”的路径。
导入系统
它解决了我的问题。