0

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?

madhu mitha
  • 3
  • 1
  • 5
  • I think you installed both "jwt" and "PyJWT" .It is not recommended to have both the "jwt" and "PyJWT" packages installed at the same time because they may cause conflicts with each other. You may want to try uninstalling the "jwt" package to see if it resolves the issue. – Hasidul Islam Jan 05 '23 at 09:49
  • Yes I did face issue before because of this. Then I uninstalled JWT and have only PyJwt with me. And also it is working fine outside the views.py function (I mean I am able to generate tokens successfully in jwttoken.py) – madhu mitha Jan 05 '23 at 10:15

2 Answers2

0

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

A. Darwin
  • 244
  • 1
  • 7
  • Yes I did face issue before because of this. Then I uninstalled JWT and have only PyJwt with me. And also it is working fine outside the **views.py** function (I mean I am able to generate tokens successfully in **jwttoken.py**) – madhu mitha Jan 05 '23 at 10:13
0

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.