"烧瓶实例"没有属性"记录"错误



>我有一个项目,我正在尝试使用具有以下结构的烧瓶和python构建一个api:

graph:
-app.py
-server.py
-apis:
-__init__.py
-users.py
-transaction_functions.py
-neo4j_ops.py

server.py文件中,我正在尝试将身份验证添加到我的 api 的端点,这些端点在users.py文件中编码。我的server.py文件如下所示:

import json
from six.moves.urllib.request import urlopen
from functools import wraps
from flask import Flask, request, jsonify, _request_ctx_stack
from flask_cors import cross_origin
from jose import jwt

AUTH0_DOMAIN = 'mydomain.eu'
API_AUDIENCE = 'https://my_audience.com'
ALGORITHMS = ["RS256"]

APP = Flask(__name__)
# Error handler
class AuthError(Exception):
def __init__(self, error, status_code):
self.error = error
self.status_code = status_code
@APP.errorhandler(AuthError)
def handle_auth_error(ex):
#some code

# Format error response and append status code
def get_token_auth_header():
"""Obtains the Access Token from the Authorization Header
"""
# some code
return token
def requires_auth(f):
"""Determines if the Access Token is valid
"""
@wraps(f)
def decorated(*args, **kwargs):
#some code
return decorated

def requires_scope(required_scope):
"""Determines if the required scope is present in the Access Token
Args:
required_scope (str): The scope required to access the resource
"""
#some code

我不断收到此错误:

Traceback (most recent call last):
File "C:Python37libsite-packagesflask_restplusapi.py", line 183, in init_app
app.record(self._deferred_blueprint_init)
AttributeError: 'Flask' object has no attribute 'record'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 16, in <module>
api.init_app(app)
File "C:Python37libsite-packagesflask_restplusapi.py", line 186, in init_app
self._init_app(app)
File "C:Python37libsite-packagesflask_restplusapi.py", line 204, in _init_app
self._register_view(app, resource, *urls, **kwargs)
File "C:Python37libsite-packagesflask_restplusapi.py", line 282, in _register_view
resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args,
AttributeError: 'function' object has no attribute 'as_view'

如您所见,此打印堆栈的结果根本没有用处,因为这些调用都不是来自我的任何文件。 其中涉及的唯一文件是 app.py,如下所示:

from flask import Flask
from flask_restplus import Api
from apis import api
import config
import os
app = Flask(__name__)
api.init_app(app)#traceback comes from here.
app.run(host='0.0.0.0', port=8080)

apis/__init__.py文件如下所示:

from flask_restplus import Api, fields
from .users import api as users
from flask import Flask

api = Api(
title='Graph Api',
version='0.2',
)
api.add_namespace(users)

知道问题是什么吗? 如果我将应用程序(烧瓶实例(从app.py导入到server.py并使用在应用程序中创建的烧瓶实例,而不是在应用程序中创建新的整个烧瓶实例,server.py错误会以某种方式消失,但问题是我会创建一个依赖项的循环调用,所以我不能这样做。

您已经有一个应用程序

APP = Flask(__name__)

您的错误处理程序至少正在使用它

然而,您定义了第二个

app = Flask(__name__)

这里的__name__app(文件名(,这可能是破坏事物的原因以及服务器文件未损坏的原因

一旦我返回到添加server.py文件之前的代码版本,问题就停止了,然后我再次添加它。我不知道问题是什么,因为代码实际上是相同的。

最新更新