Search code examples
pythonrestflaskgunicornflask-restplus

Restplus API Application object is not callable?


I'm trying to run a Restplus flask API using unicorn but i keep getting this error:

Application object must be callable.

I'm using sqlalchemy for the databse conection and models. this is my apy.py main file:

from apis import api
from database import db
import os

application = Flask(__name__)
application.config.from_object('config.DevelopmentConfig')

application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(application)

api.init_app(application)

if __name__ == '__main__':
 application.run(debug=True)

An this is how i define the init.py of the folder with the enpoints so i can get them organiced:

from flask_restplus import Api

from .status import api as status
from .cats import api as cats

api = Api(
    title='TEST API',
    version='1.0',
    description='A description',
)

api.add_namespace(status)
api.add_namespace(cats)

Im using dockers, and this is how I run gunicorn:

CMD ["gunicorn", "-b", "0.0.0.0:9000", "api:api"]

In host I get the same error. Runing the code with just python works fine. Do I need a wsig file? If so, how should I define it?

Update:

I tried running it on Windows 10 using waitress, and Ii got this error when I tried to connect ot it:

Serving on http://MR:9000
ERROR:waitress:Exception while serving /
Traceback (most recent call last):
  File "c:\users\mr\appdata\local\programs\python\python37\lib\site-packages\waitress\channel.py", line 356, in service
    task.service()
  File "c:\users\mr\appdata\local\programs\python\python37\lib\site-packages\waitress\task.py", line 172, in service
    self.execute()
  File "c:\users\mr\appdata\local\programs\python\python37\lib\site-packages\waitress\task.py", line 440, in execute
    app_iter = self.channel.server.application(environ, start_response)
TypeError: 'Api' object is not callable

So the error is when I try to connect to it?


Solution

  • Your error is that in your call to gunicorn viz.

    CMD ["gunicorn", "-b", "0.0.0.0:9000", "api:api"]

    what you are currently asking gunicorn is to look at the module api and check for a callable named api in it. Correct it to the following

    CMD ["gunicorn", "-b", "0.0.0.0:9000", "apy:application"]

    Sources:

    1. http://docs.gunicorn.org/en/stable/run.html#gunicorn