Flask app wont launch 'ImportError: cannot import name 'cached_property' from 'werkzeug' '
I've been working on a Flask app for a few weeks. I finished it today and went to deploy it... and now it won't launch.
I haven't added or removed any code so assume something has changed in the deployment process?
Anyway, here is the full error displayed in the terminal:
Traceback (most recent call last): File "C:\Users\Kev\Documents\Projects\Docket\manage.py", line 5, in <module> from app import create_app, db File "C:\Users\Kev\Documents\Projects\Docket\app\__init__.py", line 21, in <module> from app.api import api, blueprint, limiter File "C:\Users\Kev\Documents\Projects\Docket\app\api\__init__.py", line 2, in <module> from flask_restplus import Api File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\lib\site-packages\flask_restplus\__init_ _.py", line 4, in <module> from . import fields, reqparse, apidoc, inputs, cors File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\lib\site-packages\flask_restplus\fields. py", line 17, in <module> from werkzeug import cached_property ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\Kev\.virtualen vs\Docket-LasDxOWU\lib\site-packages\werkzeug\__init__.py)
Also here's the code in the three files mentioned.
from apscheduler.schedulers.background import BackgroundScheduler from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from app import create_app, db app = create_app() app.app_context().push() manager = Manager(app) migrate = Migrate(app, db) manager.add_command('db', MigrateCommand) from app.routes import * from app.models import * def clear_data(): with app.app_context(): db.session.query(User).delete() db.session.query(Todo).delete() db.session.commit() print("Deleted table rows!") @manager.command def run(): scheduler = BackgroundScheduler() scheduler.add_job(clear_data, trigger='interval', minutes=15) scheduler.start() app.run(debug=True) if __name__ == '__main__': clear_data() manager.run()
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from config import Config db = SQLAlchemy() login = LoginManager() def create_app(): app = Flask(__name__) app.config.from_object(Config) db.init_app(app) login.init_app(app) login.login_view = 'login' from app.api import api, blueprint, limiter from app.api.endpoints import users, todos, register from app.api.endpoints.todos import TodosNS from app.api.endpoints.users import UserNS from app.api.endpoints.register import RegisterNS api.init_app(app) app.register_blueprint(blueprint) limiter.init_app(app) api.add_namespace(TodosNS) api.add_namespace(UserNS) api.add_namespace(RegisterNS) return app
from logging import StreamHandler from flask_restplus import Api from flask import Blueprint from flask_limiter import Limiter from flask_limiter.util import get_remote_address blueprint = Blueprint('api', __name__, url_prefix='/api') limiter = Limiter(key_func=get_remote_address) limiter.logger.addHandler(StreamHandler()) api = Api(blueprint, doc='/documentation', version='1.0', title='Docket API', description='API for Docket. Create users and todo items through a REST API.\n' 'First of all, begin by registering a new user via the registration form in the web interface.\n' 'Or via a `POST` request to the `/Register/` end point', decorators=[limiter.limit("50/day", error_message="API request limit has been reached (50 per day)")])
I've tried reinstalling
flask_restplus but no-luck.
from werkzeug.utils import cached_property
Flask App won't start? : flask, Can you run a common HelloWorld command line application? Or do "import flask" from the python shell? Use "FLASK_APP=hello:name to specify one. I followed these instructions Anyone have this problem before? Flask app wont start. About Alumni Network Open
Downgrading to Werkzeug==0.16.1 solves this
Flask app wont start - Front End Help, Error flask.cli.NoAppException: Failed to find Flask application or factory in module "hello". Use "FLASK_APP=hello:name to specify one. Flask App won't start? If this is a better fit for r/learnpython let me know and I'll move over there. I am running the common HelloWorld app in a virtual environment and have installed flask into it.
The proper answer for May 2020: flask-restplus is dead, move to flask-restx.
flask-restplus work has been discontinued due to maintainers not having pypi keys. See the drop in replacement, flask-restx. It's an official fork by the maintainer team. We have already fixed the issue there
No. Flask-restplus is no longer maintained. The former maintainers do not have privileges to push to pypi, and after many months of trying, we forked the project. Check out flask-restx. It's a drop in replacement and we are roadmapping, designing, and making fixes...for instance, we already patched for Werkzeug
So the real solution is to move to flask-restx rather than pinning to an old version of Werkzeug.
2. What to do if the Server does not Start — Flask API, Versions of Flask older than 0.11 use to have different ways to start the application. In short, the flask command did not exist, and neither did python -m flask. To solve the problem go to Run/Debug Configuration (top right in the dropdown left the green run button) -> Tab "Configuration" -> Drop down "Environment" -> set "Environment Variables:" to FLASK_APP=run.py That will set the FLASK_APP environment variable that is missing and the framework will run again.
Downgrade Werkzeug to 0.16.1
pip3 install --upgrade Werkzeug==0.16.1
If you do a
pip3 list you may see something like this:
Flask 1.1.2 Werkzeug 0.16.1
How to Run a Flask Application, If Flask can't still find your application, then the flask run command will exit with an error. FLASK_APP=file.py : If you have your application in a� The first few experiments with Flask has been successful. A simple user interface can be created in HTML and accessed from local as well as remote clients over HTTP. One of the downsides bit us today: a web app isn’t as easy to launch as a native local application.
Quickstart — Flask Documentation (1.1.x), To run the application you can either use the flask command or python's -m switch The request object is documented in the API section and we will not cover it� Versions of Flask older than 0.11 use to have different ways to start the application. In short, the flask command did not exist, and neither did python -m flask. In that case you have two options: either upgrade to newer Flask versions or have a look at the Development Server docs to see the alternative method for running a server.
Navigate into the hello_app folder, then launch the program using python3 -m flask run (Linux/macOS) or python -m flask run (Windows). Create a container for a Flask app using the Docker extension The Docker extension makes it easy to build, manage, and deploy containerized applications from Visual Studio Code.
The first thing you see is we have defined an array of multiples quotes. These can be accessed as quote, quote, quote and so on. The function randint() returns a random number between 0 and the total number of quotes, one is subtracted because we start counting from zero.
- Looks like Werkzeug 1.0.0 was released, which has caused this error elsewhere. However Flask-Restplus is also unmaintained. You may consider moving to
flask-restxor could try pinning to
Werkzeug==0.16.1in your requirements.
- In python 3.8 you can
from functools import cached_property
- flask_restplus issue: github.com/noirbizarre/flask-restplus/issues/777
- flask-testing issue: github.com/jarus/flask-testing/issues/143
- Please remember that flask_restplus is considered dead: github.com/noirbizarre/flask-restplus/issues/770
- If anyone is having this problem with Robobrowser add it above the line 'from robobrowser import RoboBrowser' and add 'import werkzeug'