Flask 应用程序不会启动“ImportError:无法从“werkzeug”导入名称“cached_property””

Kev*_*161 28 python flask flask-restplus

我已经在 Flask 应用程序上工作了几个星期。我今天完成了它并去部署它......现在它不会启动。

我没有添加或删除任何代码,所以假设部署过程中发生了一些变化?

无论如何,这是终端中显示的完整错误:

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)
Run Code Online (Sandbox Code Playgroud)

还有这里提到的三个文件中的代码。

manage.py

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()
Run Code Online (Sandbox Code Playgroud)

app/__init__.py

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
Run Code Online (Sandbox Code Playgroud)

api/__init__.py

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)")])
Run Code Online (Sandbox Code Playgroud)

我试过重新安装flask&flask_restplus但没有运气。

nor*_*raj 19

2020 年 5 月的正确答案:flask-restplus 已死,请移至 flask-restx

来自noirbizarre/flask-restplus#778(评论)

由于维护人员没有 pypi 密钥,flask-restplus 工作已停止。查看替换品的下降,flask-restx。这是维护者团队的官方分支。我们已经在那里解决了这个问题

来自noirbizarre/flask-restplus#777(评论)

不。 Flask-restplus 不再维护。前维护者没有权限推送到 pypi,经过几个月的尝试,我们对项目进行了分叉。查看flask-restx。这是替代品的下降,我们正在制定路线图、设计和修复……例如,我们已经为 Werkzeug 打了补丁

所以真正的解决方案是转移到flask-restx而不是固定到旧版本的Werkzeug。


Chi*_*ere 15

降级到 Werkzeug==0.16.1 解决了这个问题

https://github.com/noirbizarre/flask-restplus/issues/777#issuecomment-583235327

编辑

想补充一点,永久(长期)的解决办法是转移到flask_restxflask-restplus不再被保留。

查看如何从flask-restplus迁移


Yri*_*ues 9

将 Werkzeug 降级到 0.16.1

pip3 install --upgrade Werkzeug==0.16.1
Run Code Online (Sandbox Code Playgroud)

如果你这样做,pip3 list你可能会看到这样的事情:

Flask              1.1.2
Werkzeug           0.16.1
Run Code Online (Sandbox Code Playgroud)