Cem
Cem

Reputation: 83

Django gives "502 Bad Gateway" error on Google App Engine

I'm trying to deploy a Django application on Google App Engine in the Standard Python3.8 environment. I followed all the steps in this document. App runs fine on my local with google cloud database. However I receive 502 Bad Gateway error visiting Web Url. I found a lot of different questions but technology is changing a lot, and none of them helped so far. Lastly, I came across with this solution today, but it says create a gcloud.py file that I never seen in google documents that's why I haven't applied that solution but decided to ask you.

I get this error when visiting page

Traceback (most recent call last):
_find_and_load (<frozen importlib._bootstrap>)

And this is the stack trace from the error:

File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked: ModuleNotFoundError: No module named 'main'
at _find_and_load (<frozen importlib._bootstrap>:991)
at _gcd_import (<frozen importlib._bootstrap>:1014)
at import_module (/opt/python3.8/lib/python3.8/importlib/__init__.py:127)
at import_app (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py:358)
at load_wsgiapp (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:39)
at load (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:49)
at wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py:67)
at load_wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:144)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:119)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/gthread.py:92)
at spawn_worker (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py:583)

my_site/wsgi.py

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()

This is my app.yaml

runtime: python38 
handlers:
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
 static_dir: static/
# This handler routes all requests not caught above to the main app. 
# It is required when static routes are defined, but can be omitted 
# (along with the entire handlers section) when there are no static 
# files defined.
- url: /.*
 script: auto

This is the settings.py. I tried with the commented lines but it still gives same error.

STATIC_URL = '/static/'
STATIC_ROOT = 'static'

# STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'static'),
# )
# MEDIA_URL = '/media/'
# MEDIA_ROOT = os.path.join(BASE_DIR, "media")

# STATICFILES_FINDERS = [
#    'django.contrib.staticfiles.finders.FileSystemFinder',
#    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# ]

appengine_config.py

from google.appengine.ext import vendor
vendor.add('lib')

And I have these 2 libraries in my lib folder. I moved them manually from env folder.

django
MySQLdb

Upvotes: 2

Views: 662

Answers (1)

Emmanuel
Emmanuel

Reputation: 1494

The application is complaining about not finding the main module, the document that you mentioned aboards a different problem than yours, you're not reaching the point of using the storage library.

Regarding your specific problem. As mentioned in the documentation:

The runtime starts your app by running the command you specify in the entrypoint field in your app.yaml file.

In this case your app.yaml isn't specifying an entrypoint, when this happens you have to make sure that you fulfill the next requirements:

If your app meets the following requirements, App Engine will start your app with the gunicorn web server if you don't specify the entrypoint field:

The root of your app directory contains a main.py file with a WSGI-compatible object called app.

Your app does not contain Pipfile or Pipfile.lock files.

In this case your application is lacking the main.py file which is the error App Engine is complaining about. There are multiple ways to avoid this error (like specifying an entrypoint). What I suggest in this case is to create a file named main.py on your root folder of your app and include the next:

from mysite.wsgi import application
app = application

An example containing the structure required to run a Django app can be found here

Upvotes: 1

Related Questions