ModuleNotFoundError - No module named 'main' when attempting to start service
Adding:
The main.py have to be in root of your application, where app.yaml is.
And the content, can to be, as well:
from mysite.wsgi import application
# App Engine by default looks for a main.py file at the root of the app
# directory with a WSGI-compatible object called app.
# This file imports the WSGI-compatible object of your Django app,
# application from mysite/wsgi.py and renames it app so it is discoverable by
# App Engine without additional configuration.
# Alternatively, you can add a custom entrypoint field in your app.yaml:
# entrypoint: gunicorn -b :$PORT mysite.wsgi
app = application
By default, App Engine looks for an app
variable in a file called main.py
. You have two options: put your WSGI app where App Engine expects it to be, or define a custom entrypoint:
Put your WSGI app where App Engine expects it to be:
You can create a file called main.py
that has an app
variable which is simply imported and aliased from the correct location:
from demosite.wsgi import main as app
Adding a custom entrypoint:
From https://cloud.google.com/appengine/docs/standard/python3/config/appref:
entrypoint
: Optional. The command that is executed when your app starts. For your app to receive HTTP requests,entrypoint
should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify anentrypoint
, App Engine will configure and start the Gunicorn webserver.
By default it's this:
entrypoint: gunicorn -b :$PORT main:app
You would need something like:
entrypoint: gunicorn -b :$PORT demosite.wsgi:main
See here for more details about application startup: https://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup
Simply rename your main python app (for me it was app.py) to main.py. Google Cloud requires main.py to begin the process.