Azure 部署失败:Django Azure Web 应用程序

问题描述 投票:0回答:1

我正在运行 Django Web 应用程序 - 它在本地运行,并且已成功部署到 Azure。

我现在在日志流上看到以下错误:

Traceback (most recent call last):
2024-05-03T06:02:12.640875058Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
2024-05-03T06:02:12.640878658Z     worker.init_process()
2024-05-03T06:02:12.640881958Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/workers/base.py", line 134, in init_process
2024-05-03T06:02:12.640885158Z     self.load_wsgi()
2024-05-03T06:02:12.640899358Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2024-05-03T06:02:12.640909259Z     self.wsgi = self.app.wsgi()
2024-05-03T06:02:12.640912259Z                 ^^^^^^^^^^^^^^^
2024-05-03T06:02:12.640915159Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/app/base.py", line 67, in wsgi
2024-05-03T06:02:12.640918359Z     self.callable = self.load()
2024-05-03T06:02:12.640921359Z                     ^^^^^^^^^^^
2024-05-03T06:02:12.640924259Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2024-05-03T06:02:12.640927359Z     return self.load_wsgiapp()
2024-05-03T06:02:12.640930759Z            ^^^^^^^^^^^^^^^^^^^
2024-05-03T06:02:12.640933659Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2024-05-03T06:02:12.640936759Z     return util.import_app(self.app_uri)
2024-05-03T06:02:12.640939659Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-03T06:02:12.640942759Z   File "/home/site/wwwroot/antenv/lib/python3.11/site-packages/gunicorn/util.py", line 371, in import_app
2024-05-03T06:02:12.640945959Z     mod = importlib.import_module(module)
2024-05-03T06:02:12.640948959Z           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-03T06:02:12.640952059Z   File "/opt/python/3.11.8/lib/python3.11/importlib/__init__.py", line 126, in import_module
2024-05-03T06:02:12.640955059Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-05-03T06:02:12.640957959Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-03T06:02:12.640960859Z   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-05-03T06:02:12.640964759Z   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-05-03T06:02:12.640967959Z   File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
2024-05-03T06:02:12.640971159Z   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-05-03T06:02:12.640974259Z   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-05-03T06:02:12.640977359Z   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-05-03T06:02:12.640980360Z   File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
2024-05-03T06:02:12.640984160Z ModuleNotFoundError: No module named 'trplbackend'

这是我的启动命令:

gunicorn --bind=0.0.0.0 --timeout 600 trplbackend.wsgi:application

我的项目结构经检查似乎是正确的。在本地运行此命令也有效。

我检查了 docker 日志并尝试重新部署,但似乎没有任何效果。就上下文而言,过去我的 Azure Web App 部署也很不稳定。 只需通过 Azure Pipelines 重新部署而不进行任何更改似乎就有效。

关于如何调查或修复此错误并使部署更加稳健有什么想法吗?

谢谢你。

django azure azure-web-app-service
1个回答
0
投票

我尝试将 Django 应用程序部署到 azure 应用程序服务,没有任何问题。

为了避免您遇到的错误消息:

  • 确保在部署期间正确配置并激活安装 Django 项目依赖项的虚拟环境。
  • 确保
    trplbackend
    模块存在于您的项目结构中并且可以访问。错误的路径或错误配置的模块名称可能会引起问题。

这是我的项目结构:

project_root/
├── myapp/        
├── trplbackend/   
├── venv/          
├── manage.py      
├── Procfile       
└── requirements.txt 

过程文件:

web: gunicorn --bind=0.0.0.0 trplbackend.wsgi:application

设置.py:

from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'your secretkey'
DEBUG = Tru
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
    '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 = 'trplbackend.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 = 'trplbackend.wsgi.application'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
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',
    },
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

运行以下命令,这将列出您的项目所需的所有 Python 依赖项

pip freeze > requirements.txt

本地输出:

enter image description here

我通过 Visual Studio 代码扩展部署了 Django 应用程序,并且成功,没有任何问题

enter image description here

enter image description here

日志流: enter image description here

这是输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.