Python not recognizing flask-jwt-extended module? - python

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)

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

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”.

Related

Docker not recognising psycopg2-binary as psycopg2

Using pipenv I have the following pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
flask = "*"
uuid = "*"
gunicorn = "*"
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
And then my Dockerfile is set up as follows:
FROM python:3.8.3-slim-buster
RUN useradd deploy_trial
WORKDIR /home/deploy_trial
RUN pip install pipenv
COPY . /home/deploy_trial/
RUN pipenv install --deploy
CMD ["python","./app/text.py"]
However, although the build seems to go successfully, when I run the image I get an error message saying:
Traceback (most recent call last):
File "./app/text.py", line 1, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
So it obviously thinks that psycopg2 hasn't been installed... This is really strange because when I have used psycopg2-binary on my local machine and all of my programs there recognise the installation as psycopg2 when the code is run. Does anyone know how to fix this?
My dockerfile:
FROM python:3.8.3-slim-buster
RUN python -m pip install pipenv
COPY script.py script.py
COPY Pipfile Pipfile
RUN pipenv install
CMD ["pipenv", "run", "python", "script.py"]
script.py:
import psycopg2
print('it is ok')
Pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
It works with such configuration.

ModuleNotFoundError: (already installed not working)

I can't figure out why Python will not recognize the modules. The packages are apparently installed in my pipenv according to my pipfile as you can see below.
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
beautifulsoup4 = "*"
[dev-packages]
[requires]
python_version = "3.9"
yet, when I enter
import requests
from bs4 import BeautifulSoup as bs
in my .py file, I still get ModuleNotFoundError
Thank you for any help!
EDIT: found the pipenv command equivalent to pip freeze and this also shows I have requests installed. photo below

Install needed libraries for Weasyprint on pipenv (Windows environment)

In order to start generating documents with Weasyprint I installed it on my Windows machine following these instructions:
https://weasyprint.readthedocs.io/en/stable/install.html#step-5-run-weasyprint
On my computer it works but I have a Django project where I want to integrate this library and I use pipenv. How to install the necessary libraries even in the virtual environment?
I tried setting the path for the pycairo package into the Pipfile like
pycairo= {path= "C:/Program Files/GTK3-Runtime Win64/bin/"}
but still it throws the error:
OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2 / cairo.so.2
I have 64bit Windows machine and this is the Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pylint = "*"
[packages]
django = "*"
mysql = "*"
ipython = "*"
django-webpack = "*"
django-webpack-loader = "*"
django-livereload-server = "*"
pylint = "*"
reportlab = "*"
weasyprint = "*"
django-weasyprint = "*"
pycairo= {path= "C:/Program Files/GTK3-Runtime Win64/bin/"}
cairocffi = "*"
[requires]
python_version = "3.7"
You need install 'GTK+ 64 Bit Installer' path in this local:
C:\msys2
Source: WeasyPrint Github
I came across this error as well and followed every step mentioned in the Weasyprint docs for the installation. I was using PowerShell as my default terminal and pipenv. First I tried using the command import weasyprint inside python shell in my virtualenv, but it always returned the cairo, dlopen() etc error.
What worked for me was switching to cmd. Switched and used the same commands and now it's executing perfectly.
Also, in my pipfile, for weasyprint, which is enough to generate a report in django, I only have weasyprint installed. In the question, if it is still relevant for someone, the libraries reportlab, django-weasyprint, pycairo and cairocffi can be safely removed/uninstalled from pipenv.
Please type the following command:
WHERE libcairo-2.dll
you should be getting 'C:\msys2\mingw64\bin\libcairo-2.dll'
then open your cmd and type the following.
SET PROPER_GTK_FOLDER=
SET PATH=%PROPER_GTK_FOLDER%;%PATH%
Please follow the documentation it has everything to run on windows. It worked for me it i hope it will work for youenter link description here

Question about 2 instances of package in pipfile

Im going through exercises on https://www.practicepython.org/
In ex17 there is a task to install packages "request" and "BeautifulSoup"
I did it but since an error occured i installed "BeautifulSoup" and "BeautifulSoup4"
Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
requests = "*"
beautifullsoup = "*"
beautifulsoup4 = "*"
[requires]
python_version = "3.7"
Will it couse any problems if i will import only beautifulsoup4?
Can I delete the beautifullsoup = "*" from file and it will be ok?
How can i do it?
Should i write additional commands in console?
Just run pipenv uninstall beautifullsoup to remove the installed package and you should be fine.

python pip: cannot install flask and zappa - contradicting requirements

I want to install flask and zappa, a common combination.
I use pipenv to create an evironment:
pipenv --python 3.6.4
I want to install packages:
pipenv install flask zappa
Unfortunately the requirements of flask is:
Werkzeug >= 0.14
the requirement of zappa is :
Werkzeug == 0.12
So this is not installable. What can i do?
Thanks to Evgeny this solution was successful. I could not find it in pipenv documentation.
Just manually edit the Pipfile:
Pipfile
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"
[dev-packages]
[packages]
zappa = { git = 'https://github.com/Miserlou/Zappa.git', ref = 'master', editable = true }
flask = "*"
[requires]
python_version = "3.6"

Categories