Flask RestPlus Api模块没有属性名称空间

时间:2019-03-09 08:16:06

标签: python rest api flask flask-restplus

我目前正在开始学习Flask RestPlus框架。我启动了以下代码,但无法弄清楚为什么在下面始终出现此错误。感谢您的帮助,谢谢!

堆栈跟踪:

api_1  | Traceback (most recent call last):
api_1  |   File "/app/app.py", line 3, in <module>
api_1  |     from api.main.resources.healthResource import ns as health_resource_ns
api_1  |   File "/app/api/main/resources/healthResource.py", line 4, in <module>
api_1  |     ns = api.namespace('/health', description='API Health Resource')
api_1  | AttributeError: module 'app.api' has no attribute 'namespace'

这是我的代码:

app.py

from flask import Flask, Blueprint
from flask_restplus import Api
from api.main.resources.healthResource import ns as health_resource_ns

app = Flask(__name__)
api = Api(version='1.0', title='flask-api-template',
          description='Flask RestPlus API Template Project')


def initialize_app(flask_app):
    blueprint = Blueprint('api', __name__, url_prefix='/api')

    api.init_app(blueprint)

    api.add_namespace(health_resource_ns)

    flask_app.register_blueprint(blueprint)


def run():
    initialize_app(app)
    app.run()


if __name__ == '__main__':
    run()

healthResource.py

from flask_restplus import Resource
from app import api

ns = api.namespace('/health', description='API Health Resource')


@ns.route("/")
class HealthResource(Resource):
    def get(self):
        return "Success"

1 个答案:

答案 0 :(得分:1)

循环输入存在一些问题。 app.py 导入, healthResource.py healthResource.py 导入 app.py 。您可以避免更改 healthResource.py

from flask_restplus import Resource, Namespace

ns = Namespace('health', description='API Health Resource')