Search code examples
pythongoogle-app-engineflaskgoogle-cloud-platform

Flask "(" unexpected error when using gunicorn


I tried to deploy my flask app using gcloud, gunicorn. But when I wrote the app.yaml like below, it didn't work.

Here is my app.yaml file.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT flask_todo:create_app

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

init.py file

import os
from flask import Flask,render_template,request,url_for,redirect
from . import db


def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)

    else:
        app.config.from_mapping(test_config)


    try:
        os.makedirs(app.instance_path)

    except OSError:
        pass
    return app

flask_todo #this is where I cd to and run gunicorn flask_todo:create_app

flask_todo
--db.py
--__init__.py
--app.yaml
--requirements.txt

What is the reason? Please help me..


Solution

  • Your arguments to gunicorn needs to be a variable in a module that is importable, and represents a WSGI app. In your example, create_app is a function that returns such a variable, not a variable itself.

    Something like the following should work:

    app.yaml:

    runtime: python
    env: flex
    entrypoint: gunicorn -b :$PORT flask_todo:app
    
    runtime_config:
      python_version: 3
    
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10
    

    __init__.py:

    import os
    from flask import Flask,render_template,request,url_for,redirect
    from . import db
    
    
    def create_app(test_config=None):
        app = Flask(__name__, instance_relative_config=True)
        app.config.from_mapping(
            SECRET_KEY='dev',
            DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
        )
    
        if test_config is None:
            app.config.from_pyfile('config.py', silent=True)
    
        else:
            app.config.from_mapping(test_config)
    
    
        try:
            os.makedirs(app.instance_path)
    
        except OSError:
            pass
        return app
    
    app = create_app()