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

通过GitHub部署Django到Azure遇ModuleNotFoundError问题求助

解决Azure部署Django应用时ModuleNotFoundError: No module named 'django'的问题

问题原因

Azure App Service 默认通过requirements.txt文件安装Python依赖,不会自动解析conda的environment.yml配置,因此你的Django依赖未被安装,导致应用启动失败。

解决方案

方法1:生成requirements.txt文件(推荐)

在本地激活你的SERVIR_AppTemplate conda环境,执行以下命令生成包含所有依赖的requirements.txt:

pip freeze > requirements.txt

将生成的requirements.txt文件提交到GitHub仓库,重新触发Azure部署即可。

方法2:手动从environment.yml转换requirements.txt

如果不想用pip freeze,可以直接从你的environment.yml提取依赖,手动创建requirements.txt:

python==3.9
django==4.1
django-allauth
django-import-export
netcdf4
shapely
earthengine-api
beautifulsoup4
whitenoise
aiohttp
pip==23.2.1
climateserv==0.0.24

保存为requirements.txt后提交到仓库,重新部署。

方法3:配置Azure使用conda环境(进阶)

若坚持使用conda环境,需在Azure App Service中配置自定义启动脚本:

  1. 在项目根目录创建startup.sh文件,内容如下:
#!/bin/bash
conda env create -f environment.yml
conda activate SERVIR_AppTemplate
gunicorn your_project_name.wsgi:application --bind 0.0.0.0:$PORT

注意替换your_project_name为你的Django项目名称
2. 在Azure App Service的「配置」→「常规设置」中,将「启动命令」设置为./startup.sh
3. 提交startup.sh和environment.yml到GitHub,重新部署

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 07:40:06