在connexion旁边使用python相对导入是否有特定的语法?
我目前正在尝试使用connexion构建api。但是,我在使用通过connexion模块的相对本地模块导入时遇到了一些问题,这会修改基础flask应用程序。以下是我的文件结构的简化概述:在connexion旁边使用python相对导入是否有特定的语法?,python,python-3.x,sqlalchemy,swagger,connexion,Python,Python 3.x,Sqlalchemy,Swagger,Connexion,我目前正在尝试使用connexion构建api。但是,我在使用通过connexion模块的相对本地模块导入时遇到了一些问题,这会修改基础flask应用程序。以下是我的文件结构的简化概述: 名人堂 控制器 ____初始值 routes.py 模型 ____初始值 routes.py ____初始值 config.py 创建_db.py 昂首阔步 尝试在终端中运行“python config.py”时出错。下面是config.py: import os import connexio
- 名人堂
- 控制器
- ____初始值
- routes.py
- 模型
- ____初始值
- routes.py
- ____初始值
- config.py
- 创建_db.py
- 昂首阔步
- 控制器
import os
import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
basedir = os.path.abspath(os.path.dirname(__file__))
# Create the Connexion application instance
connex_app = connexion.App(__name__, specification_dir=basedir)
# Get the underlying Flask app instance
app = connex_app.app
connex_app.add_api('swagger.yml')
# Configure the SQLAlchemy part of the app instance
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://doadmin:password@nba-player-db-do-user-7027314-0.db.ondigitalocean.com:25060/nba_test_1?sslmode=require'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Create the SQLAlchemy db instance
db = SQLAlchemy(app)
# Initialize Marshmallow
ma = Marshmallow(app)
下面是它给出的错误:
Failed to add operation for GET /api/players
Failed to add operation for GET /api/players
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 209, in add_paths
self.add_operation(path, method)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 173, in add_operation
pass_context_arg_name=self.pass_context_arg_name
File "/usr/local/lib/python3.7/site-packages/connexion/operations/__init__.py", line 8, in make_operation
return spec.operation_cls.from_spec(spec, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 137, in from_spec
**kwargs
File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 96, in __init__
pass_context_arg_name=pass_context_arg_name
File "/usr/local/lib/python3.7/site-packages/connexion/operations/abstract.py", line 96, in __init__
self._resolution = resolver.resolve(self)
File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 40, in resolve
return Resolution(self.resolve_function_from_operation_id(operation_id), operation_id)
File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 66, in resolve_function_from_operation_id
raise ResolverError(str(e), sys.exc_info())
connexion.exceptions.ResolverError: <ResolverError: module 'controller.routes' has no attribute 'read_all'>
现在我很困惑,因为我的routes.py文件确实有一个定义为read_all()的函数,下面是该文件:
from model.models import Regseason, RegSchema, Playoffs, PlayoffSchema
def read_all():
return Regseason.query.all()
我已经为这个错误绞尽脑汁了将近24小时,任何指导都将不胜感激。提前谢谢 请在operationid下面添加一个额外字段x-openapi-router-controller。Connexion使用它映射要向哪个模块发送请求。它与OperationId结合在一起,以转到正确的模块和功能。您可以链接这些路线吗?您在swagger文件中设置了一个基本路径“/api”,但Get请求会删除“/api/players”。如果您的flask路由看起来像“/api/players”,那么结果将是端点为“/api/api/players”。我实际上还没有添加任何路由,但我确实明白您的意思!我可能不应该将该文件命名为routes.py,我将修复命名约定,但您看到的最后一段代码是routes.py,我无法重现您的问题,但我相信这是由于PYTHONPATH造成的。尝试执行:
PYTHONPATH=。yourCommand
@KevinMartins我到底要用什么替换yourCommand?目录的路径?@HenryBowePYTHONPATH=。python config.py
from model.models import Regseason, RegSchema, Playoffs, PlayoffSchema
def read_all():
return Regseason.query.all()