首页 > 解决方案 > Heroku 部署:ModuleNotFoundError:没有名为“django”的模块

问题描述

将测试站点部署到 heroku 后,它显示Application Error

在此处输入图像描述

Heroku Log

2021-06-25T13:20:42.143490+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-06-25T13:20:42.143491+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-06-25T13:20:42.143492+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-06-25T13:20:42.143492+00:00 app[web.1]: self.callable = self.load()
2021-06-25T13:20:42.143492+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-06-25T13:20:42.143493+00:00 app[web.1]: return self.load_wsgiapp()
2021-06-25T13:20:42.143493+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-06-25T13:20:42.143494+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-06-25T13:20:42.143494+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
2021-06-25T13:20:42.143494+00:00 app[web.1]: mod = importlib.import_module(module)
2021-06-25T13:20:42.143495+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-06-25T13:20:42.143495+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-06-25T13:20:42.143496+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-06-25T13:20:42.143496+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-06-25T13:20:42.143496+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-06-25T13:20:42.143497+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-06-25T13:20:42.143497+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 848, in exec_module
2021-06-25T13:20:42.143498+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-06-25T13:20:42.143498+00:00 app[web.1]: File "/app/config/wsgi.py", line 12, in <module>
2021-06-25T13:20:42.143498+00:00 app[web.1]: from django.core.wsgi import get_wsgi_application
2021-06-25T13:20:42.143499+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'
2021-06-25T13:20:42.144378+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-06-25T13:20:42.184157+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [9] [INFO] Booting worker with pid: 9
2021-06-25T13:20:42.220213+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [9] [ERROR] Exception in worker process
2021-06-25T13:20:42.220214+00:00 app[web.1]: Traceback (most recent call last):
2021-06-25T13:20:42.220215+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-06-25T13:20:42.220216+00:00 app[web.1]: worker.init_process()
2021-06-25T13:20:42.220216+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-06-25T13:20:42.220216+00:00 app[web.1]: self.load_wsgi()
2021-06-25T13:20:42.220217+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-06-25T13:20:42.220217+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-06-25T13:20:42.220218+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-06-25T13:20:42.220218+00:00 app[web.1]: self.callable = self.load()
2021-06-25T13:20:42.220219+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-06-25T13:20:42.220219+00:00 app[web.1]: return self.load_wsgiapp()
2021-06-25T13:20:42.220220+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-06-25T13:20:42.220220+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-06-25T13:20:42.220220+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
2021-06-25T13:20:42.220221+00:00 app[web.1]: mod = importlib.import_module(module)
2021-06-25T13:20:42.220221+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-06-25T13:20:42.220222+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-06-25T13:20:42.220222+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-06-25T13:20:42.220222+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-06-25T13:20:42.220223+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-06-25T13:20:42.220223+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-06-25T13:20:42.220224+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 848, in exec_module
2021-06-25T13:20:42.220224+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-06-25T13:20:42.220224+00:00 app[web.1]: File "/app/config/wsgi.py", line 12, in <module>
2021-06-25T13:20:42.220225+00:00 app[web.1]: from django.core.wsgi import get_wsgi_application
2021-06-25T13:20:42.220225+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'
2021-06-25T13:20:42.222244+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-06-25T13:20:42.237399+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [4] [WARNING] Worker with pid 9 was terminated due to signal 15
2021-06-25T13:20:42.341186+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [4] [INFO] Shutting down: Master
2021-06-25T13:20:42.382466+00:00 app[web.1]: [2021-06-25 13:20:42 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-06-25T13:20:42.762358+00:00 heroku[web.1]: Process exited with status 3
2021-06-25T13:20:42.868448+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-25T13:20:42.872437+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-25T13:20:50.974740+00:00 heroku[web.1]: Starting process with command `gunicorn config.wsgi --log-file -`
2021-06-25T13:20:53.577634+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-06-25T13:20:53.578269+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [4] [INFO] Listening at: http://0.0.0.0:13141 (4)
2021-06-25T13:20:53.578415+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [4] [INFO] Using worker: sync
2021-06-25T13:20:53.583477+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [8] [INFO] Booting worker with pid: 8
2021-06-25T13:20:53.588859+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [8] [ERROR] Exception in worker process
2021-06-25T13:20:53.588860+00:00 app[web.1]: Traceback (most recent call last):
2021-06-25T13:20:53.588860+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-06-25T13:20:53.588861+00:00 app[web.1]: worker.init_process()
2021-06-25T13:20:53.588861+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-06-25T13:20:53.588861+00:00 app[web.1]: self.load_wsgi()
2021-06-25T13:20:53.588861+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-06-25T13:20:53.588862+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-06-25T13:20:53.588862+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-06-25T13:20:53.588862+00:00 app[web.1]: self.callable = self.load()
2021-06-25T13:20:53.588862+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-06-25T13:20:53.588863+00:00 app[web.1]: return self.load_wsgiapp()
2021-06-25T13:20:53.588863+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-06-25T13:20:53.588863+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-06-25T13:20:53.588864+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
2021-06-25T13:20:53.588864+00:00 app[web.1]: mod = importlib.import_module(module)
2021-06-25T13:20:53.588864+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-06-25T13:20:53.588864+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-06-25T13:20:53.588865+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-06-25T13:20:53.588865+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-06-25T13:20:53.588865+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-06-25T13:20:53.588865+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-06-25T13:20:53.588865+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 848, in exec_module
2021-06-25T13:20:53.588866+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-06-25T13:20:53.588866+00:00 app[web.1]: File "/app/config/wsgi.py", line 12, in <module>
2021-06-25T13:20:53.588866+00:00 app[web.1]: from django.core.wsgi import get_wsgi_application
2021-06-25T13:20:53.588866+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'
2021-06-25T13:20:53.588953+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-06-25T13:20:53.613376+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [4] [INFO] Shutting down: Master
2021-06-25T13:20:53.613444+00:00 app[web.1]: [2021-06-25 13:20:53 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-06-25T13:20:53.666415+00:00 heroku[web.1]: Process exited with status 3
2021-06-25T13:20:53.736085+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-25T13:21:03.827606+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=infinite-lake-64780.herokuapp.com request_id
=ebdb0566-934d-42f3-8406-3c22ad946ffe fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https
2021-06-25T13:21:04.056665+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=infinite-lake-64780.herokuapp.com
 request_id=f39bf025-1373-4119-8d11-bad44e3a7a1d fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https
2021-06-25T13:25:13.778941+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=infinite-lake-64780.herokuapp.com request_id
=cf9fc6eb-e3e5-4028-aec6-b06ebd759654 fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https
2021-06-25T13:25:14.062569+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=infinite-lake-64780.herokuapp.com
 request_id=244089c9-a74f-4fbb-a4cf-88efd26bf187 fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https
2021-06-25T13:29:24.973091+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=infinite-lake-64780.herokuapp.com request_id
=161fe80f-f695-4eff-873f-a99576f5eb6b fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https
2021-06-25T13:29:25.244494+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=infinite-lake-64780.herokuapp.com
 request_id=702bd951-0a20-47c3-9c2f-8bbefc77379f fwd="91.23.54.245" dyno= connect= service= status=503 bytes= protocol=https

这似乎是问题所在:

2021-06-25T13:20:53.588866+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'

这是root dir

在此处输入图像描述

config/settings.py

"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 3.2.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'security__key'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages',
]

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 = 'config.urls'

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

WSGI_APPLICATION = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

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


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Procfile

web: gunicorn config.wsgi --log-file -

Pipfile

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
gunicorn = "*"

[dev-packages]

[requires]
python_version = "3.8"

requirements.txt

gunicorn

runtime.txt

python-3.8.10

标签: pythondjangoherokuweb-deployment

解决方案


感谢 ENDESSA 的评论,我能够解决这个问题。我在 heroku flask webapp 部署中的 requirements.txt vs Pipfile 中找到了解决方案?.

Pipfile和似乎有问题requirements.txt。设置时pipenv,它会创建Pipfile并忽略requirements.txt. 我的问题是我venv在 Pycharm 中设置并安装了pipenv,这会导致错误。删除后,Pipfile我的测试站点正常工作。

我也必须设置heroku config:set DISABLE_COLLECTSTATIC=1,我不完全确定是否heroku ps:scale web=1也有必要。

Anyhoo,非常感谢


推荐阅读