[1043]AttributeError: module ‘jwt‘ has no attribute ‘encode‘

# 使用1
import jwt
en  = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
print(en)

# 使用2
from jwt import PyJWT

en = PyJWT().encode(payload={'some' : 'payload'}, key= 'secret',algorithm= 'HS256')
print(en)

error

Traceback (most recent call last):
  File "C:/Users/anurag.agrawal/Desktop/HackerRank/jwt/jjwwtt.py", line 3, in <module>
    en  = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
AttributeError: module 'jwt' has no attribute 'encode'

jwt报错无encode属性。经查,是由于PyJWT和JWT同时存在,导入jwt模块时出现混淆

解决方法:

卸载JWT包(PyJWT存在),尝试运行程序

# jwt卸载命令
pip uninstall jwt
# 保险起见,将PyJWT一同卸载
pip uninstall PyJWT
# 重新安装PyJWT
pip install PyJWT

参考:https://blog.csdn.net/qingliu_D/article/details/115222158
https://stackoverflow.com/questions/62997522/python-jwt-module-has-no-attribute-encode

Guess you like

Origin blog.csdn.net/xc_zhou/article/details/119810257