尝试启动服务时没有名为

尝试启动服务时没有名为

本文介绍了ModuleNotFoundError - 尝试启动服务时没有名为“main"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

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

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

My application performs flawlessly when I run it via python manage.py runserver. Yet it comes to a sudden halt when I attempt to deploy it to 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 模拟器产生了同样的问题,这是一件好事).

I've gone through numerous threads and I cannot find the problem. (For reference the dev_appserver.py emulator produces the same problem, which is a good thing).

以下是我的app.yaml

runtime: python37
env: standard

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

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

My wsgi.py file is located in the following path: demosite/wsgi.py and it's contents look like this:

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 个小时试图解决这个问题,但无济于事.

What am I missing, what am I doing wrong? I've spent close on 4 hours trying to figure this problem out to no avail.

推荐答案

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

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:

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

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

添加自定义入口点:

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

入口点:可选.在您的应用程序启动时执行的命令.为了让您的应用接收 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

See here for more details about application startup: https://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup

这篇关于ModuleNotFoundError - 尝试启动服务时没有名为“main"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:41