Forums

WSGI Question - Cannot import name 'app' (indented)

Hi running into an import error that I've been stuck at for a few hours now.

When attempting to configure my WSGI file, I'm getting an error back:

from app import app as application

ImportError: cannot import name 'app' from 'app' (/home/dan324/mysite/app.py)

If I've understood correctly, the WSGI file is trying to pick up 'app' within the same named app.py, however, my 'app' is coded within an indented section:

def create_app():
    app = Flask(__name__)
    ...
    ...

Could there be another way to import 'app'? Thanks!

You could import create_app and then set the application variable in your wsgi file to the return value of create_app

Thanks for your help glenn - I'm getting a type error now:

TypeError: create_app() takes 0 positional arguments but 2 were given

I get a syntax error with create_app(app) (fingers crossed...) so looking through the documentation and stackoverflow for any clues.

create_app does not take arguments. Don't pass app in to the function call.

Thanks glenn, here's what I'm typing to get the error:

from app import create_app as application

But I'm receiving that TypeError (I mentioned adding 'app' into the functional call as something I'd tried). Thanks again.

You need to call create_app

from app import create_app
application = create_app()

Perfect - Thanks glenn!

Glad to hear that it works for you!

You might consider putting this as an option in the web app configuration wsgi.py.

create_app() in init.py is the style shown in the Flask tutorial (v2.3).

Thanks for the suggestion.