91144


  * Django 4.0.4
  * Python 3.8.9

DjangoプロジェクトをGAEにデプロイしたのですが、"502 Bad Gateway"エラーが出ています。
GAEのログを確認したところ以下のようなエラーでした。

[code]

    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 
    
[/code]

GAEのインスタンスにTKがインストールされていないか、gunicornとdjangoとの通信が失敗しているのではないかと思っているのですが、どなたかこの問題が分かる方いますでしょうか？  
よろしくお願い致します。

\-- app.yaml --

[code]

    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
    
[/code]

\-- settings/base.py --

[code]

    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'
    
    
[/code]

\-- settings/production.py --

[code]

    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')
    
[/code]


CC BY-SA 4.0
2022-09-15T08:06:43.283
91118
2022-09-19T10:58:19.327
2022-09-19T10:58:19.327
3060
54404
question
python django tkinter google-app-engine
GAEにデプロイしたDjangoで502 Bad Gatewayになる & "libBLT.2.5.so.8.6 No such file or directory"


Google App Engine はブラウザ上で動作する環境であり、Tk
のようなデスクトップアプリ向けのライブラリを必要とするプログラムはそもそも動かないと思います。


CC BY-SA 4.0
2022-09-17T11:03:30.933
91144
2022-09-17T11:03:30.933
3060
91118
answer


こちらcubickさんの回答を参考に自己解決できました。  
`views.py`で、アプリケーションでは使わない`turtledemo`というライブラリがimportされており、これがTKを必要としていたことが原因のようです。  
PyCharmの自動補完で勝手にimport文が追加されていたようです。


CC BY-SA 4.0
2022-09-19T05:08:27.477
91171
2022-09-19T05:08:27.477
54404
91118
answer