简体   繁体   English

Python 无法识别 flask-jwt-extended 模块?

[英]Python not recognizing flask-jwt-extended module?

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;我正在 Flask API Python 3.9.6 中工作; running in a venv.在 venv 中运行。 I have pip installed and imported flask-jwt-extended for authentication purposes, but neither VSCode nor Pycharm can find the module?我安装了 pip 并导入了 flask-jwt-extended 用于身份验证,但是 VSCode 和 Pycharm 都找不到该模块? The pipfile even says that version 4.1.0 is included in the dependencies. pipfile 甚至说版本 4.1.0 包含在依赖项中。 Originally it said 3.7.0 so I tried upgrading to 4.1, but no change.最初它说 3.7.0 所以我尝试升级到 4.1,但没有变化。 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我尝试删除 JWT 和 PYJWT 并重新安装它们,在有和没有它们的情况下运行 flask-jwt-extended ...几乎所有我能想到的组合,但它一直在抛出

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:以及我在 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;根据我的阅读,flask-jwt-extended 需要 PyJWT,但我已经尝试过安装和不安装它; 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) (在 2019 年的 MacBook Pro 上,如果这很重要;人们告诉我 M1 芯片过去曾引起过问题)

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.如果你有 docker 你可以运行下面的脚本来证明它是有效的。

Therefore it must be environmental.因此它必须是环保的。 Are you definitely using the right virtualenv?您确定使用正确的 virtualenv 吗? You must run your python through pipenv for it to pick up the right enviornment, or configure your IDE as such.您必须通过 pipenv 运行您的 python 才能获得正确的环境,或者这样配置您的 IDE。

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

Well, it turns out it was the import statement.好吧,事实证明这是导入语句。 PyJWT needs to be imported as “import jwt”. PyJWT 需要导入为“import jwt”。 Further complicating the situation was the fact that flask-jwt-extended is not compatible with basic “jwt”.使情况更加复杂的是 flask-jwt-extended 与基本的“jwt”不兼容。 So uninstall jwt, install PyJWT, install flask-jwt-extended and be sure to import PyJWT as “import jwt”.所以卸载 jwt,安装 PyJWT,安装 flask-jwt-extended 并确保将 PyJWT 导入为“import jwt”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM