I am playing around with flask and connexion to set-up a REST API access for my application.
Scenario #1:
If I launch my app through Pycharm run/debug tool Everything works fine (flask app runs including the API from my agreement_api/openapi/specification.yaml
file).
Scenario #2:
But executing python agreement_api/app.py
(the main script which contains the app instance) through terminal gives me an error while binding the specification.yaml API with my flask app:
AttributeError: module 'agreement_api.controllers.agreement_controller' has no attribute 'read_agreements'
Clearly the above error message tells me that the method "read_agreeements" doesn't exist but believe me it does (because Scenario #1 is working fine and there are no typos, etc) My directory structure is as follows:
agreement_controller.py
....
def read_agreements():
agreements = Agreement.query.order_by(Agreement.agreement_status).all()
agreement_schema = AgreementSchema(many=True)
return agreement_schema.dump(agreements), 200
....
specification.yaml
servers:
- url: /api
paths:
/agreement:
get:
tags:
- Agreement
summary: Read the list of agreements
description: Read the list of agreements
x-openapi-router-controller: agreement_api.controllers.agreement_controller
operationId: read_agreements
app.py
# Get the application instance
_, connexion_app = create_app()
app = connexion_app.app # Flask instance initialized by Connexion
DB = SQLAlchemy(app)
DB.create_all()
MA = Marshmallow(app)
connexion_app.add_api("specification.yaml")
(Same is the issue when running flask app in Docker container.)
Is there an issue with my directory structure or I'm missing something other? I didn't post many questions before (should be visible by my poor elaboration/explanation/style so feel free to ask me any detail if required )
if anyone is interested in error stack trace:
Failed to add operation for GET /api/agreement
Failed to add operation for GET /api/agreement
Traceback (most recent call last):
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 209, in add_paths
self.add_operation(path, method)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 173, in add_operation
pass_context_arg_name=self.pass_context_arg_name
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/operations/__init__.py", line 8, in make_operation
return spec.operation_cls.from_spec(spec, *args, **kwargs)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/operations/openapi.py", line 138, in from_spec
**kwargs
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/operations/openapi.py", line 89, in __init__
pass_context_arg_name=pass_context_arg_name
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/operations/abstract.py", line 96, in __init__
self._resolution = resolver.resolve(self)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/resolver.py", line 40, in resolve
return Resolution(self.resolve_function_from_operation_id(operation_id), operation_id)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/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 'agreement_api.controllers.agreement_controller' has no attribute 'read_agreements'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "agreement_api/app.py", line 62, in <module>
validator_map={"body": RequestBodyValidator},
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apps/flask_app.py", line 57, in add_api
api = super(FlaskApp, self).add_api(specification, **kwargs)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apps/abstract.py", line 156, in add_api
options=api_options.as_dict())
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 111, in __init__
self.add_paths()
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 216, in add_paths
self._handle_add_operation_error(path, method, err.exc_info)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 231, in _handle_add_operation_error
raise value.with_traceback(traceback)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/resolver.py", line 61, in resolve_function_from_operation_id
return self.function_resolver(operation_id)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/utils.py", line 111, in get_function_from_name
module = importlib.import_module(module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/abdullah/Documents/projects/mine/user_agreement_flask_api/agreement_api/controllers/agreement_controller.py", line 3, in <module>
from agreement_api.app import DB
File "/home/abdullah/Documents/projects/mine/user_agreement_flask_api/agreement_api/app.py", line 62, in <module>
validator_map={"body": RequestBodyValidator},
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apps/flask_app.py", line 57, in add_api
api = super(FlaskApp, self).add_api(specification, **kwargs)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apps/abstract.py", line 156, in add_api
options=api_options.as_dict())
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 111, in __init__
self.add_paths()
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 216, in add_paths
self._handle_add_operation_error(path, method, err.exc_info)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/apis/abstract.py", line 231, in _handle_add_operation_error
raise value.with_traceback(traceback)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/resolver.py", line 61, in resolve_function_from_operation_id
return self.function_resolver(operation_id)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/utils.py", line 120, in get_function_from_name
function = deep_getattr(module, attr_path)
File "/home/abdullah/.local/share/virtualenvs/user_agreement_flask_api-7tGsycJK/lib/python3.6/site-packages/connexion/utils.py", line 68, in deep_getattr
return functools.reduce(getattr, attrs, obj)
AttributeError: module 'agreement_api.controllers.agreement_controller' has no attribute 'read_agreements'
Got the solution by my self...
Actually I just have to execute the correct command as mentioned here...
i.e instead of executing python app.py
, I have to run flask run
and my problem was solved.