模块 'hmac' 没有属性 'new'
module 'hmac' has no attribute 'new'
Python 版本 3.7.3
我正在使用 HMAC 模块。当我尝试 运行 此代码时
digest = hmac.new(s.decode("base64"), a, digestmod=hashlib.sha3_256).digest()
模块 hmac 也有一个 .digest 函数。当我 运行 .new 或 .digest 我得到这个错误:
digest = hmac.new(s.decode("base64"), a,
digestmod=hashlib.sha3_256).digest()
AttributeError: module 'hmac' has no attribute 'new'
当我运行:
pip install hmac
我也遇到这个错误:
ERROR: Command errored out with exit status 1:
command: /Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/setup.py'"'"'; __file__='"'"'/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/pip-egg-info
cwd: /private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/
Complete output (22 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/setuptools/__init__.py", line 18, in <module>
import setuptools.version
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/setuptools/version.py", line 1, in <module>
import pkg_resources
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/pkg_resources/__init__.py", line 36, in <module>
import email.parser
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/feedparser.py", line 27, in <module>
from email._policybase import compat32
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/utils.py", line 28, in <module>
import random
File "/Users/empirestrikesback/anaconda3/lib/python3.7/random.py", line 46, in <module>
from hashlib import sha512 as _sha512
File "/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/hashlib.py", line 80
raise ValueError, "unsupported hash type"
^
SyntaxError: invalid syntax
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
我假设它出于某种原因无法识别模块。我的意图是 运行 一个带有消息的 HMAC 函数,秘密,使用 SHA256。
从评论来看,您似乎找到了问题所在。您已经在本地目录中创建了一个名为 hmac.py 的文件。在您的其他脚本中,当您调用 import hmac
python 时,会在您的本地目录中找到 hmac.py 而不是 python 安装的 lib 目录中的那个。
这意味着您实际上并未加载 pythons hmac 库,而是加载了您自己的 hmac.py 脚本,该脚本没有新方法或摘要方法。
教训是不要使用可能与 pythons 或第三方模块冲突的名称调用您自己的文件,除非您专门尝试实现您自己的模块版本。
Python 版本 3.7.3
我正在使用 HMAC 模块。当我尝试 运行 此代码时
digest = hmac.new(s.decode("base64"), a, digestmod=hashlib.sha3_256).digest()
模块 hmac 也有一个 .digest 函数。当我 运行 .new 或 .digest 我得到这个错误:
digest = hmac.new(s.decode("base64"), a,
digestmod=hashlib.sha3_256).digest()
AttributeError: module 'hmac' has no attribute 'new'
当我运行:
pip install hmac
我也遇到这个错误:
ERROR: Command errored out with exit status 1:
command: /Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/setup.py'"'"'; __file__='"'"'/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/pip-egg-info
cwd: /private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/
Complete output (22 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/setuptools/__init__.py", line 18, in <module>
import setuptools.version
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/setuptools/version.py", line 1, in <module>
import pkg_resources
File "/Users/empirestrikesback/PythonProjects/practice/aPI/envAPI/lib/python3.7/site-packages/pkg_resources/__init__.py", line 36, in <module>
import email.parser
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/feedparser.py", line 27, in <module>
from email._policybase import compat32
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "/Users/empirestrikesback/anaconda3/lib/python3.7/email/utils.py", line 28, in <module>
import random
File "/Users/empirestrikesback/anaconda3/lib/python3.7/random.py", line 46, in <module>
from hashlib import sha512 as _sha512
File "/private/var/folders/7v/cp_nrj9x4w13179t2t0h61cm0000gn/T/pip-install-kdd8dbs1/hashlib/hashlib.py", line 80
raise ValueError, "unsupported hash type"
^
SyntaxError: invalid syntax
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
我假设它出于某种原因无法识别模块。我的意图是 运行 一个带有消息的 HMAC 函数,秘密,使用 SHA256。
从评论来看,您似乎找到了问题所在。您已经在本地目录中创建了一个名为 hmac.py 的文件。在您的其他脚本中,当您调用 import hmac
python 时,会在您的本地目录中找到 hmac.py 而不是 python 安装的 lib 目录中的那个。
这意味着您实际上并未加载 pythons hmac 库,而是加载了您自己的 hmac.py 脚本,该脚本没有新方法或摘要方法。
教训是不要使用可能与 pythons 或第三方模块冲突的名称调用您自己的文件,除非您专门尝试实现您自己的模块版本。