使用Gunicorn部署Django应用到Heroku遇ModuleNotFoundError求助
Django + Gunicorn 部署 Heroku 模块找不到问题解决
核心问题定位
WSGI 配置不匹配
你的settings.py中WSGI_APPLICATION配置为'content_aggregator.wsgi.application',但项目名称是weatherapp,Gunicorn命令指向的是weatherapp.wsgi,两者完全不匹配,这是导致模块找不到的直接原因。项目结构可能存在路径问题
若WSGI配置修正后仍报错,需确认项目目录结构是否符合Django标准:weatherapp/ ├── weatherapp/ # 主应用目录 │ ├── __init__.py # 必须存在,标记为Python模块 │ ├── settings.py │ ├── wsgi.py │ └── urls.py ├── manage.py ├── Procfile └── requirements.txtHeroku 环境不支持 SQLite
虽然当前报错是模块问题,但你使用的SQLite数据库在Heroku上无法持久化,后续会引发新的部署故障,必须提前修正。
分步解决方法
1. 修正 WSGI 配置
打开settings.py,将WSGI_APPLICATION改为与项目名称一致:
WSGI_APPLICATION = 'weatherapp.wsgi.application'
2. 确认项目目录结构
- 确保主应用目录(
weatherapp/weatherapp/)下存在__init__.py文件(空文件即可)。 - 本地测试时,先设置Python路径指向项目根目录,再运行Gunicorn:
export PYTHONPATH=/path/to/your/weatherapp-project gunicorn weatherapp.wsgi:application --log-file -
3. 替换 SQLite 为 PostgreSQL
Heroku仅支持PostgreSQL,修改settings.py中的数据库配置:
import dj_database_url DATABASES = { 'default': dj_database_url.config(default=os.getenv('DATABASE_URL')) }
同时在requirements.txt中添加依赖:
psycopg2-binary>=2.9.0 dj-database-url>=2.0.0
4. 重新部署 Heroku
提交所有修改后,重新推送代码:
git add . git commit -m "Fix WSGI config and database setup" git push heroku main
内容的提问来源于stack exchange,提问作者Neel Oza
相关产品推荐
相关产品推荐

