【python】JWT 报错 AttributeError: partially initialized module ‘jwt‘ has no attribute ‘encode‘

python使用jwt:

pip install pyjwt

jwt.py:

import jwt
encoded_jwt = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
jwt.decode(encoded_jwt, 'secret', algorithms=['HS256'])

报错如下:

E:\jwt>python jwt.py
Traceback (most recent call last):
  File "E:\AcademicWorkplace\DeviceIntegrity\SafetyDetect\HMSAna\jwt\jwt.py", line 1, in <module>
    import jwt
  File "E:\AcademicWorkplace\DeviceIntegrity\SafetyDetect\HMSAna\jwt\jwt.py", line 4, in <module>
    encoded_jwt = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
AttributeError: partially initialized module 'jwt' has no attribute 'encode' (most likely due to a circular import)

解决办法:

py文件不能命名为jwt.py,因为该名称是python库文件名称

参考:https://stackoverflow.com/a/46249367/19603578

I was also facing the same issue because I had named the script from which I had been calling jwt.encode() as ‘jwt.py’. So be careful while naming scripts. Try not to use any library names.

你可能感兴趣的:(Python,python,开发语言)