skip to Main Content
  • Django 4.0.4
  • Python 3.8.9

I deployed Django App to GAE.
But I got "502 Bad Gateway" error. And I checked server logs. That is below.

Traceback (most recent call last):
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
   worker.init_process()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
   self.load_wsgi()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
   self.wsgi = self.app.wsgi()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
   self.callable = self.load()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
   return self.load_wsgiapp()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
   return util.import_app(self.app_uri)
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
   mod = importlib.import_module(module)
 File "/opt/python3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
 File "<frozen importlib._bootstrap>", line 991, in _find_and_load
 File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 843, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "/workspace/SakeMarksV1/wsgi.py", line 16, in <module>
   application = get_wsgi_application()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
   django.setup(set_prefix=False)
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
   apps.populate(settings.INSTALLED_APPS)
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/apps/registry.py", line 116, in populate
   app_config.import_models()
 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/apps/config.py", line 304, in import_models
   self.models_module = import_module(models_module_name)
 File "/opt/python3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
 File "<frozen importlib._bootstrap>", line 991, in _find_and_load
 File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 843, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "/workspace/website/models.py", line 1, in <module>
   import turtledemo.nim
 File "/opt/python3.8/lib/python3.8/turtledemo/nim.py", line 13, in <module>
   import turtle
 File "/opt/python3.8/lib/python3.8/turtle.py", line 107, in <module>
   import tkinter as TK
 File "/opt/python3.8/lib/python3.8/tkinter/__init__.py", line 36, in <module>
   import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libBLT.2.5.so.8.6: cannot open shared object file: No such file or directory 

I’m thinking that GAE instance has no TK or gunicorn is not working.
Do anyone know about this kind of problem?
Thank you.

— app.yaml —

runtime: python38

instance_class: F1

env: standard

entrypoint: gunicorn -b :$PORT <PROJECT_NAME>.wsgi:application

includes:
    - secret.yaml

handlers:
    - url: /static
      static_dir: static/
    - url: /.*
      script: auto
      secure: always

automatic_scaling:
  max_instances: 2

— settings/base.py —

from pathlib import Path
import os
from dotenv import load_dotenv

load_dotenv()

BASE_DIR = Path(__file__).resolve().parent.parent.parent

AUTH_USER_MODEL = 'website.User'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'website',
    'widget_tweaks',
    'social_django',
    'corsheaders',
    'django_cleanup',
    'stdimage',
    'storages',
]

MIDDLEWARE = [
    'django.middleware.gzip.GZipMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = '<PROJECT_NAME>.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]

WSGI_APPLICATION = '<PROJECT_NAME>.wsgi.application'

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

AUTHENTICATION_BACKENDS = (
    'social_core.backends.open_id.OpenIdAuth',
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

USE_I18N = True

USE_TZ = True

PROJECT_NAME = os.path.basename(BASE_DIR)

STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = f'var/www/{PROJECT_NAME}/static'


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

LOGIN_REDIRECT_URL = 'index'
LOGOUT_REDIRECT_URL = 'index'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

— settings/production.py —

from .base import *

SECRET_KEY = os.getenv('SECRET_KEY')

DEBUG = False

ALLOWED_HOSTS = ['<CUSTOM_DOMAIN>']

# データベース
import pymysql

pymysql.install_as_MySQLdb()

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '/cloudsql/' + os.getenv('DB_CONNECTION_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'NAME': os.getenv('DB_NAME'),
    }
}


# ストレージ
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    os.path.join(BASE_DIR, '<PROJECT_NAME>/settings/<CREDENTIAL_FILE>.json')
)

DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'

GS_BUCKET_NAME = os.getenv('GS_BUCKET_NAME')
GS_PROJECT_ID = '<PROJECT_NAME>'

# ソーシャルログイン
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_KEY')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.getenv('SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET')

SOCIAL_AUTH_FACEBOOK_KEY = os.getenv('SOCIAL_AUTH_FACEBOOK_KEY')
SOCIAL_AUTH_FACEBOOK_SECRET = os.getenv('SOCIAL_AUTH_FACEBOOK_SECRET')

3

Answers


  1. Chosen as BEST ANSWER

    I solved this problem by myself.
    There was the code import turtledemo on views.py and the library needs TK. That was the cause of the error.
    PyCharm automatically added this code.
    Thank you, Supun Kaihara and NoCommandLine.


  2. Like you mentioned, the error might be from tkinter, try this:

    Deploy keras model to google app engine

    Login or Signup to reply.
  3. Updated

    Change your entrypoint to just

    entrypoint: gunicorn -b :$PORT <PROJECT_NAME>.wsgi
    
    

    Since you have an entrypoint in your app.yaml file which is run via gunicorn, you must explicitly include gunicorn in requirements.txt file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search