gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:48 25 4
gpt4 key购买 nike

上下文:

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

最佳答案

默认情况下,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: Optional. The command that is executed when your app starts. For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify an entrypoint, App Engine will configure and start the Gunicorn webserver.

默认是这样的:

entrypoint: gunicorn -b :$PORT main:app

你需要这样的东西:

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

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

关于python - ModuleNotFoundError - 尝试启动服务时没有名为 'main' 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52395695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com