Home > database >  Python not recognizing flask-jwt-extended module?
Python not recognizing flask-jwt-extended module?

Time:02-21

I have a very annoying problem that I've been trying to fix all day. I'm working on a Flask API in Python 3.9.6; running in a venv. I have pip installed and imported flask-jwt-extended for authentication purposes, but neither VSCode nor Pycharm can find the module? The pipfile even says that version 4.1.0 is included in the dependencies. Originally it said 3.7.0 so I tried upgrading to 4.1, but no change. I tried removing JWT and PYJWT and reinstalling them, running flask-jwt-extended with and without them... just about every combination I can think of, but it just keeps throwing

ModuleNotFoundError: No module named 'flask_jwt_manager'

every. single. time. I've visited quite a few sites and some people seem to have run into the same situation and were able to resolve it through various means, but none have worked in my case. Does anyone have any idea on what is going on here? Here is my pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*" 
flask-cors = "*"
bson = "*"
flask-pymongo = "*"
pymongo = {extras = ["srv"], version = "*"}
dnspython = "*"
flask-jwt-extended = "==4.3.1"
pyjwt = "*"

[dev-packages]

[requires]
python_version = "3.9"

and my imports at the top of my api:

import datetime
import json

import pymongo
from flask_jwt_extended import JWTManager
from bson.objectid import ObjectId
from flask import Flask, jsonify, make_response, Response, request
from flask_cors import CORS
from pymongo import ReturnDocument
from werkzeug.security import generate_password_hash, check_password_hash

From what I've read, flask-jwt-extended requires PyJWT, but I've tried it both with and without it installed; no luck. I'm a newbie, so forgive me if I'm missing something stupid. (On a 2019 MacBook Pro if that matters; people have told me the M1 chip has caused issues in the past)

CodePudding user response:

So the good news is there's nothing wrong with the dependcies; if you have docker you can run the script below to prove it is working.

Therefore it must be environmental. Are you definitely using the right virtualenv? You must run your python through pipenv for it to pick up the right enviornment, or configure your IDE as such.

PROJECT_NAME=check-pipenv

PYTHON_VERSION=3.9.6

cd "$(mktemp -d)" || exit

cat << EOF > Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*"
flask-cors = "*"
bson = "*"
flask-pymongo = "*"
pymongo = {extras = ["srv"], version = "*"}
dnspython = "*"
flask-jwt-extended = "==4.3.1"
pyjwt = "*"

[dev-packages]

[requires]
python_version = "${PYTHON_VERSION}"
EOF

cat << 'EOF' > ${PROJECT_NAME}.py
import datetime
import json

import pymongo
from flask_jwt_extended import JWTManager
from bson.objectid import ObjectId
from flask import Flask, jsonify, make_response, Response, request
from flask_cors import CORS
from pymongo import ReturnDocument
from werkzeug.security import generate_password_hash, check_password_hash
print('Working')
EOF

cat << EOF > Dockerfile
FROM python:${PYTHON_VERSION}-slim-buster
WORKDIR /workdir
COPY Pipfile Pipfile
RUN pip install pipenv
RUN pipenv install && pipenv run pip freeze
COPY . .
CMD [ "pipenv", "run", "python", "${PROJECT_NAME}.py"]
EOF

DOCKER_BUILDKIT=0 docker build --tag ${PROJECT_NAME}:latest .
docker run --rm --name ${PROJECT_NAME} ${PROJECT_NAME}:latest

prints:

Working

CodePudding user response:

Well, it turns out it was the import statement. PyJWT needs to be imported as “import jwt”. Further complicating the situation was the fact that flask-jwt-extended is not compatible with basic “jwt”. So uninstall jwt, install PyJWT, install flask-jwt-extended and be sure to import PyJWT as “import jwt”. Thanks for the help and all glory to documentation.

  • Related