You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署Django应用失败:Procfile配置报错求助

Django部署Heroku崩溃:Gunicorn无法找到application属性

问题描述

尝试将Django应用部署到Heroku,但应用启动即崩溃,怀疑是Procfile配置问题。先后尝试了两种Procfile配置:

  1. web: gunicorn SC_BASIS:application -b 127.0.0.1:8000
  2. web: gunicorn SCBASIS.wsgi

两种配置均失败,核心错误提示为Failed to find attribute 'application' in 'SC_BASIS',完整错误日志如下:

Starting process with command `gunicorn SC_BASIS:application -b 127.0.0.1:8000`
2023-12-22T22:28:22.256483+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [2] [INFO] Starting gunicorn 21.2.0
2023-12-22T22:28:22.256752+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [2] [INFO] Listening at: http://127.0.0.1:8000 (2)
2023-12-22T22:28:22.256788+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [2] [INFO] Using worker: sync
2023-12-22T22:28:22.258739+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [7] [INFO] Booting worker with pid: 7
2023-12-22T22:28:22.259865+00:00 app[web.1]: Failed to find attribute 'application' in 'SC_BASIS'.
2023-12-22T22:28:22.259934+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [7] [INFO] Worker exiting (pid: 7)
2023-12-22T22:28:22.280621+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [8] [INFO] Booting worker with pid: 8
2023-12-22T22:28:22.280808+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [2] [ERROR] Worker (pid:7) exited with code 4
2023-12-22T22:28:22.281142+00:00 app[web.1]: Failed to find attribute 'application' in 'SC_BASIS'.
2023-12-22T22:28:22.281204+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [8] [INFO] Worker exiting (pid: 8)
2023-12-22T22:28:22.300841+00:00 app[web.1]: [2023-12-22 22:28:22 +0000] [2] [ERROR] Worker (pid:8) exited with code 4
2023-12-22T22:28:22.300880+00:00 app[web.1]: Traceback (most recent call last):
2023-12-22T22:28:22.300892+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 202, in run
2023-12-22T22:28:22.301100+00:00 app[web.1]: self.manage_workers()
2023-12-22T22:28:22.301151+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 571, in manage_workers
2023-12-22T22:28:22.301250+00:00 app[web.1]: self.spawn_workers()
2023-12-22T22:28:22.301269+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 643, in spawn_workers
2023-12-22T22:28:22.301371+00:00 app[web.1]: time.sleep(0.1 * random.random())
2023-12-22T22:28:22.301397+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2023-12-22T22:28:22.301463+00:00 app[web.1]: self.reap_workers()
2023-12-22T22:28:22.301481+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 533, in reap_workers
2023-12-22T22:28:22.301569+00:00 app[web.1]: raise HaltServer(reason, self.APP_LOAD_ERROR)
2023-12-22T22:28:22.301602+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'App failed to load.' 4>
2023-12-22T22:28:22.301611+00:00 app[web.1]: 
2023-12-22T22:28:22.301611+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2023-12-22T22:28:22.301619+00:00 app[web.1]: 
2023-12-22T22:28:22.301620+00:00 app[web.1]: Traceback (most recent call last):
2023-12-22T22:28:22.301630+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 8, in <module>
2023-12-22T22:28:22.301676+00:00 app[web.1]: sys.exit(run())
2023-12-22T22:28:22.301743+00:00 app[web.1]: ^^^^^
2023-12-22T22:28:22.301752+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
2023-12-22T22:28:22.301841+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2023-12-22T22:28:22.301864+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/app/base.py", line 236, in run
2023-12-22T22:28:22.301959+00:00 app[web.1]: super().run()
2023-12-22T22:28:22.301975+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/app/base.py", line 72, in run
2023-12-22T22:28:22.302015+00:00 app[web.1]: Arbiter(self).run()
2023-12-22T22:28:22.302035+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 229, in run
2023-12-22T22:28:22.302101+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2023-12-22T22:28:22.302128+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 342, in halt
2023-12-22T22:28:22.302243+00:00 app[web.1]: self.stop()
2023-12-22T22:28:22.302260+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 396, in stop
2023-12-22T22:28:22.302332+00:00 app[web.1]: time.sleep(0.1)
2023-12-22T22:28:22.302350+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2023-12-22T22:28:22.302404+00:00 app[web.1]: self.reap_workers()
2023-12-22T22:28:22.302420+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/gunicorn/arbiter.py", line 533, in reap_workers
2023-12-22T22:28:22.302502+00:00 app[web.1]: raise HaltServer(reason, self.APP_LOAD_ERROR)
2023-12-22T22:28:22.302530+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'App failed to load.' 4>
2023-12-22T22:28:22.469709+00:00 heroku[web.1]: Process exited with status 1
2023-12-22T22:28:22.497223+00:00 heroku[web.1]: State changed from starting to crashed
2023-12-22T22:28:55.730377+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=scbasis-a8315e618fcf.herokuapp.com request_id=ee714226-d27e-4c0e-8370-b5520ea232c4 fwd="216.9.110.7" dyno= connect= service= status=503 bytes= protocol=https
2023-12-22T22:28:56.220149+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=scbasis-a8315e618fcf.herokuapp.com request_id=f88097dd-caeb-439d-bf21-933a6ce30c42 fwd="216.9.110.7" dyno= connect= service= status=503 bytes= protocol=https

问题根源与修复方案

1. 核心问题

  • Gunicorn无法找到指定模块中的application属性:你直接指向SC_BASIS模块,但Django的WSGI应用对象默认定义在项目名.wsgi模块中,而非项目根模块本身。
  • 端口绑定错误:硬编码127.0.0.1:8000会导致应用仅能在本地访问,Heroku要求应用监听0.0.0.0并使用环境变量$PORT指定的端口。

2. 修复步骤

步骤1:确认项目结构

确保你的项目结构符合Django默认规范(以SCBASIS为项目根目录):

项目根目录/
├── SCBASIS/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py       # 包含application对象的文件
├── manage.py
├── Procfile
└── requirements.txt

步骤2:修正Procfile配置

将Procfile内容替换为:

web: gunicorn SCBASIS.wsgi --bind 0.0.0.0:$PORT
  • SCBASIS.wsgi:指向Django项目根目录下的wsgi模块,Gunicorn会自动识别其中的application对象
  • --bind 0.0.0.0:$PORT:绑定到所有网络接口,使用Heroku分配的端口,避免硬编码

步骤3:验证WSGI文件

检查SCBASIS/wsgi.py,确保包含以下内容(Django默认生成):

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'SCBASIS.settings')

application = get_wsgi_application()

如果文件缺失或内容错误,重新生成项目(django-admin startproject SCBASIS)。

步骤4:重新部署

提交修改后的Procfile并重新部署:

git add Procfile
git commit -m "Fix Procfile for Heroku deployment"
git push heroku main

内容的提问来源于stack exchange,提问作者Salamander 27

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 14:14:56