PGHE
PGHE

Reputation: 1962

Error with WSGI when deploying Flask App to Azure

I keep getting the following error when deploying an Azure Web using Flask:

Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\home\python364x64\wfastcgi.py", line 791, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\home\python364x64\wfastcgi.py", line 633, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
  File "D:\home\python364x64\wfastcgi.py", line 603, in get_wsgi_handler
    handler = getattr(handler, name)
AttributeError: module 'app' has no attribute 'app'

This is the structure of my app:

|-app
    |- __init__.py
    |- mod1
    |- mod2
|-config.py
|-runserver.py
|-web.config

This is my web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.app"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

config.py:

import os

app_dir = os.path.abspath(os.path.dirname(__file__))

class BaseConfig:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'A SECRET KEY'

class DevelopementConfig(BaseConfig):
    DEBUG = True

runserver.py:

import os
from app import db, create_app

app = create_app(os.getenv('FLASK_ENV') or 'config.DevelopementConfig')


if __name__ == '__main__':   
    app.run()

and app.init.py:

from flask import Flask
def create_app(config):
    app = Flask(__name__)
    app.config.from_object(config)
    return app

I tried including a wsgi.py with:

from app import app as application

at the same level as runserver.py but still got the above error. What am I missing here?

Upvotes: 1

Views: 2288

Answers (1)

Fine
Fine

Reputation: 2154

Well, how does WSGI work? It starts, imports your app and invokes it's methods to handle requests. <add key="WSGI_HANDLER" value="app.app"/> for WSGI means import module app and get variable app from it. But after importing your app.__init__.py there are only Flask and create_app variables, having Flask-class and create_app-function. So you should in WSGI_HANDLER set value pointing to flask app, either runserver.app or app.create_app() (I don't know how exactly, with correct syntax, Azure handles app as a function, but it surely can).

Upvotes: 3

Related Questions