【问题标题】:Heroku deployment: ModuleNotFoundError: No module named 'django'Heroku 部署:ModuleNotFoundError:没有名为“django”的模块
【发布时间】:2021-09-08 21:43:39
【问题描述】:

将测试站点部署到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

【问题讨论】:

  • 您正在尝试使用/导入 django,但它不在您的要求中,因此未安装。
  • 在你的 requirements.txt 中添加这个Django==3.2.3 并尝试再次推送
  • @AnkitTiwari 我将 django==3.2.3 添加到 requirements.txt 但仍然得到完全相同的错误
  • 您是否通过 git 暂存、提交和推送您的更改到 Heroku?
  • 我认为这可能会有所帮助stackoverflow.com/questions/63252388/…

标签: python django heroku web-deployment


【解决方案1】:

问题是 Django 不在您的 requirements.txt 文件中。您必须在 requirements.txt 文件中包含您使用的所有模块。

要解决此问题,请将您的 requirements.txt 文件更改为包含 Django(以及您使用的任何其他模块)

您的 requirements.txt 文件应如下所示(如果您没有任何其他模块):

django
gunicorn

如果您仍然遇到同样的错误,请删除您的 Pipfile,并确保您已通过 git 暂存、提交并推送您的更改到 Heroku。

要暂存、提交和推送,请在命令行中执行以下操作(确保您位于应用的目录中)

进行更改:

git add .

提交更改:

git commit -m 'Updated requirements.txt'

将您的更改推送到 Heroku:

git push heroku master

【讨论】:

  • 我按照您的指示,将django 添加到requirements.txt,使用git addgit commit 提交并推送到heroku,但我仍然遇到同样的错误。
【解决方案2】:

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

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

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

好的,非常感谢

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2021-08-04
    • 2021-06-07
    • 2019-12-16
    • 1970-01-01
    • 2021-03-26
    • 2021-03-07
    • 2021-08-30
    • 2020-08-06
    相关资源
    最近更新 更多