我有一个 django 应用程序,当我使用“python manage.py runserver”在本地运行它时,它可以正常工作。然而,Heroku 正在努力让该网站正常运行。
\n我将更改提交到 git,这会触发 Heroku 上的自动构建。构建成功,但在尝试使用 Gunicorn 命令启动进程时失败。我在下面粘贴了完整的 Heroku 日志,但相关位似乎是“ModuleNotFoundError:没有名为“FromThePath.apps”的模块”,这似乎是指 settings.py 中的 INSTALLED_APPS。
\n我尝试将 INSTALLED_APPS 更改为带有额外的“FromThePath”前缀。(这解决了 wsgi.py 中的错误),但这会导致在构建期间使用collectstatic命令失败,并显示“ModuleNotFoundError:没有名为'FromThePath.FromThePath'的模块”
\n不知道从这里去哪里。我的 Google-fu 显然不够。在此先感谢您的帮助!
\n完整的 Heroku 日志:
\n2021-03-09T17:01:30.000000+00:00 app[api]: Build succeeded\n2021-03-09T17:01:40.327614+00:00 heroku[web.1]: Starting process with command `gunicorn FromThePath.FromThePath.wsgi`\n2021-03-09T17:01:44.495636+00:00 app[web.1]: [2021-03-09 17:01:44 +0000] [4] [INFO] Starting gunicorn 20.0.4\n2021-03-09T17:01:44.497096+00:00 app[web.1]: [2021-03-09 17:01:44 +0000] [4] [INFO] Listening at: http://0.0.0.0:49607 (4)\n2021-03-09T17:01:44.497258+00:00 app[web.1]: [2021-03-09 17:01:44 +0000] [4] [INFO] Using worker: sync\n2021-03-09T17:01:44.510099+00:00 app[web.1]: [2021-03-09 17:01:44 +0000] [9] [INFO] Booting worker with pid: 9\n2021-03-09T17:01:44.547692+00:00 app[web.1]: [2021-03-09 17:01:44 +0000] [10] [INFO] Booting worker with pid: 10\n2021-03-09T17:01:45.165418+00:00 heroku[web.1]: State changed from starting to up\n2021-03-09T17:01:45.478113+00:00 app[web.1]: [2021-03-09 11:01:45 -0600] [9] [ERROR] Exception in worker process\n2021-03-09T17:01:45.478159+00:00 app[web.1]: Traceback (most recent call last):\n2021-03-09T17:01:45.478161+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker\n2021-03-09T17:01:45.478162+00:00 app[web.1]: worker.init_process()\n2021-03-09T17:01:45.478163+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process\n2021-03-09T17:01:45.478163+00:00 app[web.1]: self.load_wsgi()\n2021-03-09T17:01:45.478164+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi\n2021-03-09T17:01:45.478164+00:00 app[web.1]: self.wsgi = self.app.wsgi()\n2021-03-09T17:01:45.478165+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi\n2021-03-09T17:01:45.478165+00:00 app[web.1]: self.callable = self.load()\n2021-03-09T17:01:45.478166+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load\n2021-03-09T17:01:45.478166+00:00 app[web.1]: return self.load_wsgiapp()\n2021-03-09T17:01:45.478167+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp\n2021-03-09T17:01:45.478167+00:00 app[web.1]: return util.import_app(self.app_uri)\n2021-03-09T17:01:45.478167+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app\n2021-03-09T17:01:45.478168+00:00 app[web.1]: mod = importlib.import_module(module)\n2021-03-09T17:01:45.478168+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module\n2021-03-09T17:01:45.478169+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)\n2021-03-09T17:01:45.478170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import\n2021-03-09T17:01:45.478170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load\n2021-03-09T17:01:45.478170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked\n2021-03-09T17:01:45.478171+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked\n2021-03-09T17:01:45.478171+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module\n2021-03-09T17:01:45.478172+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\n2021-03-09T17:01:45.478173+00:00 app[web.1]: File "/app/FromThePath/FromThePath/wsgi.py", line 16, in <module>\n2021-03-09T17:01:45.478173+00:00 app[web.1]: application = get_wsgi_application()\n2021-03-09T17:01:45.478173+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application\n2021-03-09T17:01:45.478174+00:00 app[web.1]: django.setup(set_prefix=False)\n2021-03-09T17:01:45.478174+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup\n2021-03-09T17:01:45.478174+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)\n2021-03-09T17:01:45.478175+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate\n2021-03-09T17:01:45.478175+00:00 app[web.1]: app_config = AppConfig.create(entry)\n2021-03-09T17:01:45.478175+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 116, in create\n2021-03-09T17:01:45.478176+00:00 app[web.1]: mod = import_module(mod_path)\n2021-03-09T17:01:45.478176+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module\n2021-03-09T17:01:45.478176+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)\n2021-03-09T17:01:45.478177+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import\n2021-03-09T17:01:45.478177+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load\n2021-03-09T17:01:45.478177+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked\n2021-03-09T17:01:45.478186+00:00 app[web.1]: ModuleNotFoundError: No module named 'FromThePath.apps'\n2021-03-09T17:01:45.478671+00:00 app[web.1]: [2021-03-09 11:01:45 -0600] [9] [INFO] Worker exiting (pid: 9)\n2021-03-09T17:01:45.542581+00:00 app[web.1]: [2021-03-09 11:01:45 -0600] [10] [ERROR] Exception in worker process\n2021-03-09T17:01:45.542583+00:00 app[web.1]: Traceback (most recent call last):\n2021-03-09T17:01:45.542584+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker\n2021-03-09T17:01:45.542585+00:00 app[web.1]: worker.init_process()\n2021-03-09T17:01:45.542585+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process\n2021-03-09T17:01:45.542585+00:00 app[web.1]: self.load_wsgi()\n2021-03-09T17:01:45.542586+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi\n2021-03-09T17:01:45.542586+00:00 app[web.1]: self.wsgi = self.app.wsgi()\n2021-03-09T17:01:45.542587+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi\n2021-03-09T17:01:45.542587+00:00 app[web.1]: self.callable = self.load()\n2021-03-09T17:01:45.542588+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load\n2021-03-09T17:01:45.542588+00:00 app[web.1]: return self.load_wsgiapp()\n2021-03-09T17:01:45.542588+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp\n2021-03-09T17:01:45.542589+00:00 app[web.1]: return util.import_app(self.app_uri)\n2021-03-09T17:01:45.542589+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app\n2021-03-09T17:01:45.542589+00:00 app[web.1]: mod = importlib.import_module(module)\n2021-03-09T17:01:45.542592+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module\n2021-03-09T17:01:45.542592+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)\n2021-03-09T17:01:45.542593+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import\n2021-03-09T17:01:45.542593+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load\n2021-03-09T17:01:45.542594+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked\n2021-03-09T17:01:45.542594+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked\n2021-03-09T17:01:45.542594+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module\n2021-03-09T17:01:45.542595+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\n2021-03-09T17:01:45.542595+00:00 app[web.1]: File "/app/FromThePath/FromThePath/wsgi.py", line 16, in <module>\n2021-03-09T17:01:45.542595+00:00 app[web.1]: application = get_wsgi_application()\n2021-03-09T17:01:45.542596+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application\n2021-03-09T17:01:45.542597+00:00 app[web.1]: django.setup(set_prefix=False)\n2021-03-09T17:01:45.542598+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup\n2021-03-09T17:01:45.542598+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)\n2021-03-09T17:01:45.542598+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate\n2021-03-09T17:01:45.542599+00:00 app[web.1]: app_config = AppConfig.create(entry)\n2021-03-09T17:01:45.542599+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 116, in create\n2021-03-09T17:01:45.542600+00:00 app[web.1]: mod = import_module(mod_path)\n2021-03-09T17:01:45.542600+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module\n2021-03-09T17:01:45.542600+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)\n2021-03-09T17:01:45.542601+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import\n2021-03-09T17:01:45.542601+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load\n2021-03-09T17:01:45.542602+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked\n2021-03-09T17:01:45.542608+00:00 app[web.1]: ModuleNotFoundError: No module named 'FromThePath.apps'\n2021-03-09T17:01:45.543290+00:00 app[web.1]: [2021-03-09 11:01:45 -0600] [10] [INFO] Worker exiting (pid: 10)\n2021-03-09T17:01:45.752608+00:00 app[web.1]: [2021-03-09 17:01:45 +0000] [4] [INFO] Shutting down: Master\n2021-03-09T17:01:45.752780+00:00 app[web.1]: [2021-03-09 17:01:45 +0000] [4] [INFO] Reason: Worker failed to boot.\n2021-03-09T17:01:45.912133+00:00 heroku[web.1]: Process exited with status 3\n2021-03-09T17:01:46.066521+00:00 heroku[web.1]: State changed from up to crashed\nRun Code Online (Sandbox Code Playgroud)\n我的文件夹结构:
\nFromThePath // root folder\n\xc2\xb3 .gitignore\n\xc2\xb3 .slugignore\n\xc2\xb3 package.json\n\xc2\xb3 Procfile\n\xc2\xb3 requirements.txt\n\xc2\xb3 ToThePath.bat\n\xc2\xb3 tree.txt\n\xc2\xb3 \n.idea // in .gitignore\nFromThePath // it's not clear from the formatting, but this is a subfolder of the FromThePath root folder\n\xc2\xb3 \xc2\xb3 .coverage\n\xc2\xb3 \xc2\xb3 .coveragerc\n\xc2\xb3 \xc2\xb3 db.postgresql\n\xc2\xb3 \xc2\xb3 db.sqlite3\n\xc2\xb3 \xc2\xb3 manage.py\n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 cooking\n\xc2\xb3 \xc2\xb3 \xc2\xb3 admin.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 apps.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 forms.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 models.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 tests.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 urls.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 views.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 __init__.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 {other presumably irrelevant folders}\n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 DevNotes\n\xc2\xb3 \xc2\xb3 {same as cooking}\n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 FromThePath\n\xc2\xb3 \xc2\xb3 \xc2\xb3 apps.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 asgi.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 forms.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 settings.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 urls.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 views.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 wsgi.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 __init__.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 static\n\xc2\xb3 \xc2\xb3 \xc2\xb3 FromThePath\n\xc2\xb3 \xc2\xb3 \xc2\xb3 ...\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 templates\n\xc2\xb3 \xc2\xb3 \xc2\xb3 FromThePath\n\xc2\xb3 \xc2\xb3 \xc2\xb3 *.html files\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 templatetags\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \xc2\xb3 general_filters.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \xc2\xb3 __init__.py\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 journal\n\xc2\xb3 \xc2\xb3 {same as cooking}\n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 polls\n\xc2\xb3 \xc2\xb3 {same as cooking}\n\xc2\xb3 \xc2\xb3 \n\xc2\xb3 staticfiles\n\xc2\xb3 templates\n\xc2\xb3 \xc2\xb3 admin\n\xc2\xb3 \xc2\xb3 \xc2\xb3 base_site.html\n\xc2\xb3 \xc2\xb3 \xc2\xb3 \n\xc2\xb3 \xc2\xb3 registration\n\xc2\xb3 \xc2\xb3 logged_out.html\n\xc2\xb3 \xc2\xb3 login.html\n\xc2\xb3 \xc2\xb3 password_reset_complete.html\n\xc2\xb3 \xc2\xb3 password_reset_confirm.html\n\xc2\xb3 \xc2\xb3 password_reset_done.html\n\xc2\xb3 \xc2\xb3 password_reset_email.html\n\xc2\xb3 \xc2\xb3 password_reset_form.html\n\xc2\xb3 \xc2\xb3 \nvenv (in .gitignore)\nRun Code Online (Sandbox Code Playgroud)\n设置.py
\nimport os\nimport django_heroku\n\n# Build paths inside the project like this: os.path.join(BASE_DIR, ...)\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n\n# Quick-start development settings - unsuitable for production\n# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/\n\n# SECURITY WARNING: keep the secret key used in production secret!\nSECRET_KEY = '[redacted]'\n\n# SECURITY WARNING: don't run with debug turned on in production!\nDEBUG = True\n\nALLOWED_HOSTS = []\n\n\n# Application definition\n\nINSTALLED_APPS = [\n\n 'django.contrib.admin',\n 'django.contrib.auth', # Core authentication framework and its default models.\n 'django.contrib.contenttypes', # Django content type system (allows permissions to be associated with models).\n 'django.contrib.sessions',\n 'django.contrib.messages',\n 'django.contrib.staticfiles',\n\n 'FromThePath.apps.FromThePathConfig',\n 'cooking.apps.CookingConfig',\n 'journal.apps.JournalConfig',\n 'DevNotes.apps.DevnotesConfig',\n 'polls.apps.PollsConfig',\n\n # # Build fails on "python FromThePath/manage.py collectstatic --noinput" step\n # # relevant part of error: ModuleNotFoundError: No module named 'FromThePath.FromThePath'\n # 'FromThePath.FromThePath.apps.FromThePathConfig',\n # 'FromThePath.cooking.apps.CookingConfig',\n # 'FromThePath.journal.apps.JournalConfig',\n # 'FromThePath.DevNotes.apps.DevnotesConfig',\n # 'FromThePath.polls.apps.PollsConfig',\n\n]\n\nMIDDLEWARE = [\n 'django.middleware.security.SecurityMiddleware',\n 'django.contrib.sessions.middleware.SessionMiddleware', # Manages sessions across requests\n 'django.middleware.common.CommonMiddleware',\n 'django.middleware.csrf.CsrfViewMiddleware',\n 'django.contrib.auth.middleware.AuthenticationMiddleware', # Associates users with requests using sessions.\n 'django.contrib.messages.middleware.MessageMiddleware',\n 'django.middleware.clickjacking.XFrameOptionsMiddleware',\n]\n\nROOT_URLCONF = 'FromThePath.urls'\n\nTEMPLATES = [\n {\n 'BACKEND': 'django.template.backends.django.DjangoTemplates',\n 'DIRS': [os.path.join(BASE_DIR, 'templates')],\n 'APP_DIRS': True,\n 'OPTIONS': {\n 'context_processors': [\n 'django.template.context_processors.debug',\n 'django.template.context_processors.request',\n 'django.contrib.auth.context_processors.auth',\n 'django.contrib.messages.context_processors.messages',\n 'FromThePath.DevNotes.context_processors.dev_note_form_context_processor',\n ],\n 'libraries':{\n 'general_filters': 'FromThePath.templatetags.general_filters',\n }\n },\n },\n]\n\nWSGI_APPLICATION = 'FromThePath.wsgi.application'\n\n\n# Database\n# https://docs.djangoproject.com/en/3.0/ref/settings/#databases\n\nDATABASES = {\n 'default': {\n [redacted]\n }\n}\n\n\n# Password validation\n# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators\n\nAUTH_PASSWORD_VALIDATORS = [\n {\n 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',\n },\n {\n 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',\n },\n {\n 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',\n },\n {\n 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',\n },\n]\n\n\n# Internationalization\n# https://docs.djangoproject.com/en/3.0/topics/i18n/\n\nLANGUAGE_CODE = 'en-us'\n\nTIME_ZONE = 'America/Chicago'\n\nUSE_I18N = True\n\nUSE_L10N = True\n\nUSE_TZ = True\n\n\n# Static files (CSS, JavaScript, Images)\n# https://docs.djangoproject.com/en/3.0/howto/static-files/\n\nSTATIC_URL = '/static/'\n\n# Redirect to home URL after login (Default redirects to /accounts/profile/)\nLOGIN_REDIRECT_URL = '/'\n\n# Site isn't capable of email yet. This makes it so that the stuff that would be emailed is logged to the Django Console\n# starting point to actually implement: https://docs.djangoproject.com/en/3.1/topics/email/\nEMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'\n\ndjango_heroku.settings(locals())\nRun Code Online (Sandbox Code Playgroud)\n过程文件
\nweb: gunicorn FromThePath.FromThePath.wsgi\nRun Code Online (Sandbox Code Playgroud)\nwsgi.py
\nimport os\n\nfrom django.core.wsgi import get_wsgi_application\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FromThePath.FromThePath.settings')\n\napplication = get_wsgi_application()\nRun Code Online (Sandbox Code Playgroud)\n.gitignore
\n*.pyc\nvenv/*\n.idea/*\nFromThePath/htmlcov\nRun Code Online (Sandbox Code Playgroud)\n
通过更改 Procfile 使其运行在与 manage.py 相同的目录中。您可以使用gunicorn的chrdir或pythonpath选项来做到这一点
web: gunicorn --chdir FromThePath FromThePath.wsgi
Run Code Online (Sandbox Code Playgroud)
或者
web: gunicorn --pythonpath FromThePath FromThePath.wsgi
Run Code Online (Sandbox Code Playgroud)
我还必须从几个地方删除项目名称(wsgi.py 和 settings.py 中的上下文处理器)