Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
furqan-asghar opened this issue Aug 16, 2018 · 12 comments
Closed

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

furqan-asghar opened this issue Aug 16, 2018 · 12 comments

Comments

@furqan-asghar
Copy link

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'])

@yosida95
Copy link
Member

It's a caller bug. jwt.JWT.encode is not a static method, you should instantiate jwt.JWT class first and then call encode method of that instance.

@peterwoo339
Copy link

can anyone shed a light? I've got the same issue..

@yosida95
Copy link
Member

yosida95 commented Mar 27, 2020

You should first instantiate the jwt.JWT class, then call the encode instance method of the instance.

import jwt

instance = jwt.JWT()
instance.encode(...)

There is also a complete encoding/decoding code example on the README. Refer https://github.com/GehirnInc/python-jwt#examples.

@nwoow
Copy link

nwoow commented Aug 12, 2020

You should first instantiate the jwt.JWT class, then call the encode instance method of the instance.

import jwt

instance = jwt.JWT()
instance.encode(...)

There is also a complete encoding/decoding code example on the README. Refer https://github.com/GehirnInc/python-jwt#examples.

I have installed this using:
pip install jwt

And I am using a code:

 import jwt
 instance = jwt.JWT()
 encoded_jwt = instance.encode({'some': 'payload'}, 'secret', algorithm='HS256')

But I am getting error:

'function' object has no attribute 'JWT'

@yosida95
Copy link
Member

@nwoow Please paste full Python traceback

@nwoow
Copy link

nwoow commented Aug 12, 2020

@nwoow Please paste full Python traceback

I am using Django and I have installed it and I am trying to generate a token using code above and the response I am getting:
I am getting this error:

instance = jwt.JWT()
AttributeError: 'function' object has no attribute 'JWT'

@yosida95
Copy link
Member

yosida95 commented Aug 12, 2020

I think that there is a function named jwt (def jwt(...) or jwt = ...) in your script which imports the jwt library because if the jwt imported correctly, the jwt variable in your code should reference a module object, not a function object.

AttributeError: 'function' object has no attribute 'JWT'

@nwoow
Copy link

nwoow commented Aug 12, 2020

@yosida95 I have changed my function name to something different actually in views.py controller name was def jwt so changed it to the other name and I am getting this error:

 instance = jwt.JWT()
AttributeError: module 'jwt' has no attribute 'JWT'

@yosida95
Copy link
Member

yosida95 commented Aug 12, 2020

@nwoow could you provide the output of pip freeze? I think you've installed both jwt and PyJWT. Due to known name clash issue (#9), both libraries cannot co-exist. Please also see this issue comment #11 (comment).

@nwoow
Copy link

nwoow commented Aug 12, 2020

@yosida95 yes I am using getstream client and they are using PyJWT both PyJWT and your jwt both are importing jwt how come. Do you have any solution

@yosida95
Copy link
Member

Unfortunately, no. As of now both libraries use the same jwt module namespace and Python's module system cannot resolve import jwt deterministically.

@nwoow
Copy link

nwoow commented Aug 12, 2020

@yosida95 anyway I fixe the problem by uninstalling using :

pip uninstall jwt==1.0.0

And then I uninstalled pyJWT using:

pip uninstall PyJWT

And then install it again:

pip install PyJWT

And my code:

encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')

Is working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants