Pratap Alok Raj
Pratap Alok Raj

Reputation: 1206

Flask Version Incompatibility issue

I am a beginner to Flask and I am developing a small API for my team using Flask.

Earlier when I was using Python 3.8 and pip older version (I am not sure what version that was), my app.py was running fine. But whenever I did a pip install <SOME_PACKAGE_NAME>, I used to get a message stating to upgrade my pip, and so I upgraded it to pip 21.1.1. After upgrading the pip, I also updated Python 3.8 to Python 3.9 and created a new Python virtual environment, and tried installing the below three libraries there that are required for my application:

  1. Flask
  2. Flask-RESTful
  3. Flask-JWT

But I saw that my Flask and other packages have the latest versions, so I tried running my app in the new virtual environment, but it keeps failing. I am adding below the steps along with the failure message and a few other info.

(base) Apples-MacBook-Pro:pythonEnvs testuser$ virtualenv -p python3.9 venvTest

(base) Apples-MacBook-Pro:pythonEnvs testuser$ cd ../Documents/Personal/FlaskProject/src/Flask-Api/

(base) Apples-MacBook-Pro:Flask-Api testuser$ source ~/pythonEnvs/venvTest/bin/activate

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip --version
pip 21.1.1 from /Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/pip (python 3.9)

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ python --version
Python 3.9.5

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install flask

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install Flask-RESTful

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ pip install Flask-JWT

(venvTest) (base) Apples-MacBook-Pro:Flask-Api testuser$ python3.9 app.py 

Traceback (most recent call last):
  File "/Users/testuser/Documents/githubProjects/FlaskProject/src/Flask-Api/app.py", line 2, in <module>
    from flask_restful import Api
  File "/Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/flask_restful/__init__.py", line 14, in <module>
    from flask.helpers import _endpoint_from_view_func
ImportError: cannot import name '_endpoint_from_view_func' from 'flask.helpers' (/Users/testuser/pythonEnvs/venvTest/lib/python3.9/site-packages/flask/helpers.py)

I am fortunate enough to have the pip freeze from the older pip version, so I pasted that into requirements.txt and do an explicit pip install on it, and am running the application for now on it. But what exactly is the issue with the new Flask and other dependencies version.

Below are the results of pip freeze from older pip and after upgrading and installing libraries in the new virtual environment:

# Pip Freeze from an older version

aniso8601==9.0.1
click==7.1.2
Flask==1.1.2
Flask-JWT==0.3.2
Flask-RESTful==0.3.8
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
PyJWT==1.4.2
pytz==2021.1
six==1.16.0

# Pip Freeze from the new version ( pip 21.1.1 )

aniso8601==9.0.1
click==8.0.0
Flask==2.0.0
Flask-JWT==0.3.2
Flask-RESTful==0.3.8
itsdangerous==2.0.0
Jinja2==3.0.0
MarkupSafe==2.0.0
PyJWT==1.4.2
pytz==2021.1
six==1.16.0
Werkzeug==2.0.0

Below is my app.py which I am trying to run:-

from flask import Flask
from flask_restful import Api
from flask_jwt import JWT
from security import authenticate, identity
from resources.user import UserRegister
from resources.item import Item, ItemList


# Resources are something our API is concerned with ( let it be students, items etc)
app = Flask(__name__)
app.secret_key = 'flask_application'
api = Api(app)

jwt = JWT(app, authenticate, identity) # JWT creates a new endpoint /auth

api.add_resource(Item, '/item/<string:name>')
api.add_resource(ItemList, '/items')
api.add_resource(UserRegister, '/register')

if __name__ == "__main__":
    app.run(port=5000, debug = True) # To debug use (debug = True)

(Authenticate and identity are functions used to authenticate users from our DB. UserRegister, Item, and ItemList are classes with some pre-defined attribution required for the respective endpoints).

Upvotes: 1

Views: 7932

Answers (1)

J&#252;rgen Gmach
J&#252;rgen Gmach

Reputation: 6123

Flask-Restful was no longer compatible with the recently released Flask 2.0.

A fix was applied some hours ago:

https://github.com/flask-restful/flask-restful/commit/fc9b34c39472284a57c50d94fec5b51fe8d71e14

There is no new release on PyPI yet.

You could either install Flask-Restful via GitHub or wait some time for a new release and meanwhile use your older pinned versions.

Upvotes: 1

Related Questions