pythonflaskwaitress

"app" module not found when running flask app using waitress


I would like to run my flask app using waitress and NXING. Here's part of my project structure:

 |__webapp     
    ├── templates
    │   └── index.html
    └── __init__.py
    └── app.py
 |main.py
 |__init__.py
 | ...

Until now I always ran my flask application like so: FLASK_APP=app.py flask run without any trouble. Now I want to start using waitress so I created a main.py file looking something like this:

from waitress import serve

sys.path.append('~/[path]/webapp')
from .. import app

if __name__ == "__main__":
    serve(app, host="0.0.0.0", port=5000)

When I want to launch my server via the cmd-prompt with waitress-serve app:app the following exception appears:

There was an exception (ModuleNotFoundError) importing your module

It had these arguments:
1. No module named "app"

I can imagine that the problem occurs when importing "app" from an overlying folder but I'm not able to fix the problem.

EDIT: I forgot to cd to the webapp folder in my project directory but when trying to start the web application a new error occurs:

There was an exception (ModuleNotFoundError) importing your module

It had these arguments:
1. attempted relative import with no known parent package

EDIT 2.0: The full structure can be seen below:

 |__webapp     
    ├── templates
    │   └── index.html
    │   └── dashboard.html
    ├── static
    │   └── styles
    │       └── ...
    │   └── webfonts
    │       └── ...
    └── __init__.py
    └── app.py
 |main.py
 |__init__.py
 |tracking.py
 |results.csv

There really isn't much more to it than this. Btw, both init.py files are empty

I appreciate any help!


Solution

  • Try modifying the following:

    webapp/__init__.py:

    from .app import app
    

    main.py:

    from webapp import app
    

    and from the root folder of your project, which i suppose is the same folder that contains the main.pyfile:

    $ waitress-serve main:app