Michael Michael - 3 months ago 14
Python Question

Running Flask with Gunicorn raises TypeError: index() takes 0 positional arguments but 2 were given

I'm trying to run a Flask app with Gunicorn. When I run

gunicorn app:index
, I get the error
TypeError: index() takes 0 positional arguments but 2 were given
. None of the examples I've seen show
index
needing two parameters. What does this error mean? How do I run Flask with Gunicorn?

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
return 'Hello, World!'




gunicorn app:index




respiter = self.wsgi(environ, resp.start_response)
TypeError: index() takes 0 positional arguments but 2 were given


I changed
index
to take two arguments, but got a different error.

@app.route("/")
def index(arg1, arg2):
return 'Hello, World!'




/python3.4/site-packages/flask/templating.py, line 132, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

vrs vrs
Answer

You have to pass a Flask instance to gunicorn, not a function name like you did with index. So if your app saved as app.py, then you have to run gunicorn as follows:

$ gunicorn --bind 127.0.0.1:8000 app:app