GehirnInc/python-jwt

module 'jwt' has no attribute 'encode'

Allan-Nava opened this issue ยท 10 comments

I want to encode with jwt.encode but I got this error.

Yes. The module doesn't export encode function, so an error you reported should occur.

As README, you must instantiate jwt.JWT class first, then use encode method of the instance.

AttributeError: 'dict' object has no attribute '_jws'

got this error :

File "E:\github\MyGits\TDA\rest_server_sql.py", line 113, in login
token = jwt.JWT.encode({'public_id' : user.public_id, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])
File "E:\github\MyGits\TDA\venv_tda\lib\site-packages\jwt\jwt.py", line 44, in encode
return self._jws.encode(message, key, alg, optional_headers)
AttributeError: 'dict' object has no attribute '_jws'

while generating token:
token = jwt.JWT.encode({'public_id' : user.public_id, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])

@furqan-asghar Your problem is not relative to this one, and as you're already doing, your problem will be discussed on #15.

After putting JWT before encode this is happening

AttributeError: module 'jwt' has no attribute 'JWT'

@ranjannandi Please provide minimum reproducible code, and also jwt version you're using.

I hit a similar issue with a test script in a filename of jwt.py - renamed it and it worked.

use "pip install pyjwt"

you can check whether your code is name "jwt.py",if so,change it

Your script is named jwt.py, or jwt.py exists in the same directory as the script

Rename jwt.py, or import jwt imports your script instead of the jwt library.

Both the jwt library and PyJWT are installed

There is a known name crash #9. Use either the jwt or PyJWT exclusively.

Despite the above, the problem still exists

Make sure you are calling the encode method of a jwt.JWT() instance. See #15 and the README example.

If this is not the case, please file an issues.