pythonpython-3.xdjangojwtauth0

PyJWT installed but keey getting: ModuleNotFoundError: No module named 'jwt'


My Django project was working fine. Then followed the steps to implement Auth0 into my project, which I have setup on my frontend successfully. I followed all of these steps: https://auth0.com/docs/quickstart/backend/django/01-authorization

Running in Python 3.9.6 ('.venv': Pipenv) ./.venv/bin/python Trying installing requirements in all environments as well enter image description here

I even updated requirements.txt to the most modern versions to see if it was a compatibility issue. When I run python manage.py runserver I get

  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/chasesheaff/teachr-backend/teachr/urls.py", line 23, in <module>
    path('', include('auth0authorization.urls')),
  File "/Users/chasesheaff/Library/Python/3.9/lib/python/site-packages/django/urls/conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/chasesheaff/teachr-backend/auth0authorization/urls.py", line 3, in <module>
    from . import views
  File "/Users/chasesheaff/teachr-backend/auth0authorization/views.py", line 5, in <module>
    import jwt
ModuleNotFoundError: No module named 'jwt'

pip freeze yields this result

asgiref==3.7.2
certifi==2024.7.4
cffi==1.17.0
chardet==3.0.4
charset-normalizer==3.3.2
cryptography==43.0.0
Django==4.2.15
django-cors-headers==4.4.0
djangorestframework==3.10.3
drf-jwt==1.19.2
future==0.18.2
idna==2.8
macholib==1.15.2
pycparser==2.22
PyJWT==2.9.0
requests==2.32.3
six==1.15.0
sqlparse==0.4.4
typing_extensions==4.8.0
urllib3==1.25.11```

have jwt in ```utils.py```
```from django.contrib.auth import authenticate
import json
import jwt
import requests

def jwt_get_username_from_payload_handler(payload):
    username = payload.get('sub').replace('|', '.')
    authenticate(remote_user=username)
    return username
... more code in file

and in views.py


# Create your views here.
from functools import wraps
import jwt

from django.http import JsonResponse

def get_token_auth_header(request):
    """Obtains the Access Token from the Authorization Header
    """
    auth = request.META.get("HTTP_AUTHORIZATION", None)
    parts = auth.split()
    token = parts[1]

    return token
... more code

Solution

  • I'm a frontend engineer trying to learn backend even deeper. So I will explain it through that lens to help anyone else in that similar situation.

    pip is similar to npm requirements.txt is similar to package.json Better explanation here: Is npm in Node like virtualenv in Django?

    I have python 3.9 installed as well as 3.11.5 I was running the virtual environment with python manage.py runserver which was running python 3.9, however when I used python3 manage.py runserver, which uses 3.11.5 all worked as expected.

    Days of coding lost, but lots learned about virtual environments. This thread here helped lead to the solution: https://discuss.python.org/t/python3-v3-x-vs-python-v3-x/29087