简体   繁体   English

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

[英]Flask RestPlus Api module has no attribute namespace

I am currently starting to learn the Flask RestPlus framework. 我目前正在开始学习Flask RestPlus框架。 I have the following code I started, but I cannot figure out why I keep getting this error below. 我启动了以下代码,但无法弄清楚为什么在下面始终出现此错误。 I appreciate any help, thanks! 感谢您的帮助,谢谢!

Stack Trace: 堆栈跟踪:

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'

Here is my code: 这是我的代码:

app.py 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 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"

You have some problems with circular imports. 循环导入存在一些问题。 app.py imports and healthResource.py and healthResource.py imports app.py . app.py进口和healthResource.pyhealthResource.py进口app.py。 You can avoid this changing the healthResource.py : 您可以避免这种更改healthResource.py

from flask_restplus import Resource, Namespace

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM