Home > Software design >  'import jwt' not working in Django views.py though I have PyJwt installed
'import jwt' not working in Django views.py though I have PyJwt installed

Time:01-06

I'm working with Django and trying to generate jwt tokens in views.py and displaying it in html page. import jwt is throwing No module found error in views.py even though I have PyJwt installed already inside the virtual environment and it is working fine outside the views.py (say, jwttoken.py file)

views.py

import jwt
def generateJWT(portal):
    key = 'secret'
    payload =  {'iat': time.time(),
                'iss' : 'localhost',
                'exp': int(time.time())   (5*365*24 * 60),
                'portal': portal}  
    #return payload
    return jwt.encode(payload, key, algorithm="HS256")

Does it mean that I can't make use of jwt module in django? Is there any other way to make it work in Django as it is working outside?

CodePudding user response:

jwt and PyJWT are two different modules. They are both imported with "import jwt", so they can conflict with each other.

Check which JWT module you have installed. If you have both try removing one of them.

See also: PyJWT won't import jwt.algorithms (ModuleNotFoundError: No module named 'jwt.algorithms')

CodePudding user response:

u can check it by running 'pip freeze' in terminal to check if its installed or not, if 'jwt' is not listed then it means its not available in your pj. Or you can try import pyjwt instead of import jwt to check whether if its working or not.

Otherwise, there might be a problem with django project itself. make sure u activate the exact environment.

  • Related