Docker部署Django启动报ModuleNotFoundError找不到temperhum.apps
Django Docker部署出现
No module named 'temperhum.apps'报错解决 问题现象
Django项目在本地非Docker调试模式下可正常运行,容器化部署启动时抛出模块找不到的错误,核心报错信息:
ModuleNotFoundError: No module named 'temperhum.apps'
相关配置参考
项目目录结构
xxxProjectv3 │ ├── asgi.py │ ├── celery.py │ ├── __init__.py │ ├── settings │ │ ├── development_settings.py │ │ ├── django_settings.py │ │ ├── __init__.py │ │ ├── production_settings.py │ │ └── project_settings.py │ ├── urls.py │ └── wsgi.py ├── CCTV │ ├── admin.py │ ├── apps.py │ ├── camera.py │ ├── consumers.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── routing.py │ ├── tasks.py │ ├── tests.py │ ├── urls.py │ └── views.py ├── celerybeat-schedule.db ├── db.sqlite3 ├── docker-compose.yml ├── Dockerfile ├── dome_control │ ├── admin.py │ ├── apps.py │ ├── conditions.py │ ├── consumers.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── relays.py │ ├── routing.py │ ├── tasks.py │ ├── tests.py │ ├── urls.py │ └── views.py ├── home │ ├── admin_bak.py │ ├── apps.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ └── views.py ├── manage.py ├── requirements.txt ├── shared_data │ ├── conditions.json │ ├── debug.log │ ├── django.log │ ├── relays.json │ └── temperhum.json ├── static │ ├── bootstrap.bundle.js │ ├── bootstrap.bundle.js.map │ ├── bootstrap.css │ ├── bootstrap.css.map │ ├── grey.css │ ├── home.css │ ├── jquery-3.2.1.js │ ├── main.css │ ├── reconnecting-websocket.js │ ├── video.js │ └── video-js.css ├── temperhum │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tasks.py │ ├── tests.py │ └── views.py
原Dockerfile配置
# pull official base image, call it "base" FROM python:3.9.13-bullseye as base RUN apt update -y && apt upgrade -y # set work directory RUN mkdir -p /xx/devATP WORKDIR /xx/devATP #Prevents Python from writing pyc files to disc ENV PYTHONDONTWRITEBYTECODE 1 #Prevents Python from buffering stdout and stderr ENV PYTHONUNBUFFERED 1 FROM base as django # install venv RUN python3 -m venv /xx/devATP/venv # enable venv ENV PATH="/xx/devATP/venv/bin:$PATH" # install dependencies RUN pip install --upgrade pip COPY requirements.txt /xx/devATP/ # special pip install as in docker install doesn't call piwheels # and tries to make a number of dependencies from scratch (crypotgraphy fails) RUN pip3 install --index-url=https://www.piwheels.org/simple --no-cache-dir -r requirements.txt COPY . /xx/devATP
原docker-compose.yml核心配置
services: daphne: image: django:latest container_name: daphne privileged: true command: daphne -b 0.0.0.0 -p 8000 xxxProjectv3.asgi:application # command: python manage.py runserver 0.0.0.0:8000 environment: - settings_file=production #- settings_file=development ports: - "8000:8000" volumes: - static:/xxx/devATP/static - data:/xxx/devATP/shared_data #copy logs restart: on-failure depends_on: - redis
完整报错日志
daphne | 2022-06-12 12:17:58,115 [INFO] django.utils.autoreload: Watching for file changes with StatReloader daphne | Exception in thread django-main-thread: daphne | Traceback (most recent call last): daphne | File "/usr/local/lib/python3.9/threading.py", line 980, in _bootstrap_inner daphne | self.run() daphne | File "/usr/local/lib/python3.9/threading.py", line 917, in run daphne | self._target(*self._args, **self._kwargs) daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper daphne | fn(*args, **kwargs) daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run daphne | autoreload.raise_last_exception() daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception daphne | raise _exception[1] daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 398, in execute daphne | autoreload.check_errors(django.setup)() daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper daphne | fn(*args, **kwargs) daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/__init__.py", line 24, in setup daphne | apps.populate(settings.INSTALLED_APPS) daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate daphne | app_config = AppConfig.create(entry) daphne | File "/xxx/devATP/venv/lib/python3.9/site-packages/django/apps/config.py", line 213, in create daphne | mod = import_module(mod_path) daphne | File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module daphne | return _bootstrap._gcd_import(name[level:], package, level) daphne | File "<frozen importlib._bootstrap>", line 1030, in _gcd_import daphne | File "<frozen importlib._bootstrap>", line 1007, in _find_and_load daphne | File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked daphne | ModuleNotFoundError: No module named 'temperhum.apps'
根因分析
- 路径配置不一致+旧镜像缓存:Dockerfile中配置的工作目录是
/xx/devATP(2个x),但docker-compose中挂载卷的路径、报错日志里的实际运行路径都是/xxx/devATP(3个x),说明修改Dockerfile路径后未重新构建镜像,容器仍使用旧版本镜像,旧镜像中代码文件不全或路径映射错误。 - 文件名大小写问题:本地开发如果用Windows/macOS系统,文件名大小写不敏感,即使
apps.py命名存在大小写错误(比如Apps.py、APP.py)本地也能正常导入;但Linux容器内核大小写敏感,文件名不匹配会直接报模块找不到。 - 构建排除规则问题:如果项目根目录下的
.dockerignore配置了错误的排除规则,会导致构建镜像时漏掉temperhum/apps.py文件。 - 源适配问题:Dockerfile中pip安装用的piwheels源是ARM架构(树莓派等设备)专用源,x86架构服务器用该源安装依赖会出现架构不兼容,也可能触发异常导入错误。
修复步骤
- 统一路径配置,简化路径避免拼写错误
把Dockerfile和docker-compose里的工作目录统一改成简单的/app,修改后的Dockerfile参考:
对应修改docker-compose.yml配置,增加build字段确保从本地Dockerfile构建,统一挂载路径:FROM python:3.9.13-bullseye as base RUN apt update -y && apt upgrade -y WORKDIR /app ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # 显式将项目根目录加入Python搜索路径 ENV PYTHONPATH=/app FROM base as django RUN python3 -m venv /app/venv ENV PATH="/app/venv/bin:$PATH" RUN pip install --upgrade pip COPY requirements.txt /app/ # x86架构部署删掉--index-url参数,ARM架构(树莓派)保留 RUN pip3 install --no-cache-dir -r requirements.txt COPY . /appservices: daphne: build: . image: django:latest container_name: daphne privileged: true working_dir: /app command: daphne -b 0.0.0.0 -p 8000 xxxProjectv3.asgi:application environment: - settings_file=production ports: - "8000:8000" volumes: - static:/app/static - data:/app/shared_data restart: on-failure depends_on: - redis - 检查本地文件合法性
- 确认
temperhum/apps.py文件名全小写,无拼写错误 - 确认
temperhum目录下存在__init__.py文件,且无语法错误 - 检查
.dockerignore文件,删除所有可能排除apps.py文件的规则
- 确认
- 清理旧缓存重新构建
执行以下命令彻底清理旧镜像和容器,重新构建启动:docker-compose down docker rmi django:latest docker-compose up --build -d - 异常排查
如果仍报错,进入容器内部检查文件是否存在:
确认输出列表中存在docker exec -it daphne ls -la /app/temperhumapps.py和__init__.py文件即可。
内容的提问来源于stack exchange,提问作者SgtBilko
相关产品推荐
相关产品推荐

