ModuleNotFoundError - 尝试启动服务时没有名为“main”的模块

新手上路,请多包涵

语境:

  • 我使用 Python 3.7 创建了一个 Django 应用程序。
  • 我正在(尝试)使用第二代 Google App Engine 标准环境。

当我通过 python manage.py runserver 运行它时,我的应用程序运行完美。然而,当我尝试将其部署到 Google App Engine 时,它突然停止了。

 Traceback (most recent call last):
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/tmp/tmphgUsp3/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
ModuleNotFoundError: No module named 'main'

我经历了很多线程,但找不到问题所在。 (参考 dev_appserver.py 模拟器产生同样的问题,这是一件好事)。

以下是我的 app.yaml

 runtime: python37
env: standard

handlers:
- url: /static
  static_dir: static/
- url: .*
  script: demosite.wsgi.main

我的 wsgi.py 文件位于以下路径: demosite/wsgi.py 其内容如下所示:

 import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demosite.settings')

main = get_wsgi_application()

我的 settings.py 文件:

 import os

class AppSettings(object):
    GoogleCloudProject = os.getenv('GOOGLE_CLOUD_PROJECT')

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'say what?'

DEBUG = True

ALLOWED_HOSTS = [
    '*'
]

INSTALLED_APPS = [
    'anchor.apps.AnchorConfig',
    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    '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',
]

ROOT_URLCONF = 'demosite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'demosite.wsgi.main'

try:
    import MySQLdb
except ImportError:
    import pymysql
    pymysql.install_as_MySQLdb()

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'webapp',
            'USER': 'aasdeytst',
            'PASSWORD': 'asdasygetasfasdfasd.',
            'HOST': 'asdgiuasfivaasd',
            'PORT': '3306'
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'webapp',
            'USER': 'awthdsfhfdhdf',
            'PASSWORD': 'asdasdasdagwdatwt',
            'HOST': 'localhost',
            'PORT': '3306'
        }
    }

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',
    },
]

ANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

CRISPY_TEMPLATE_PACK = 'bootstrap4'

LOGIN_REDIRECT_URL = 'index'
LOGIN_URL = 'login'

SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_COOKIE_AGE = 1800

我错过了什么,我做错了什么?我花了近 4 个小时试图解决这个问题,但无济于事。

原文由 Marley 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 583
2 个回答

默认情况下,App Engine 在名为 main.py 的文件中查找 app 变量。您有两个选择:将您的 WSGI 应用程序放在 App Engine 期望的位置,或者定义一个自定义入口点:

将您的 WSGI 应用程序放在 App Engine 期望的位置:

您可以创建一个名为 main.py 的文件,其中有一个 app 变量,它只是从正确的位置导入和别名:

 from demosite.wsgi import main as app

添加自定义入口点:

来自 https://cloud.google.com/appengine/docs/standard/python3/config/appref

entrypoint :可选。应用程序启动时执行的命令。为了让您的应用程序接收 HTTP 请求, entrypoint 应该包含一个命令,用于启动一个 Web 服务器,该服务器侦听由 PORT 环境变量指定的端口。如果您不指定 entrypoint ,App Engine 将配置并启动 Gunicorn 网络服务器。

默认情况下是这样的:

 entrypoint: gunicorn -b :$PORT main:app

你需要这样的东西:

 entrypoint: gunicorn -b :$PORT demosite.wsgi:main

有关应用程序启动的更多详细信息,请参见此处: https ://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup

原文由 Dustin Ingram 发布,翻译遵循 CC BY-SA 4.0 许可协议

添加:

main.py 必须位于应用程序的根目录中,app.yaml 所在的位置。

内容也可以是:

    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

原文由 Miguel Herreros Cejas 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题