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

Django+Docker环境下出现ModuleNotFoundError:找不到widget_tweaks模块

Django + Docker环境下django-widget-tweaks未被识别的解决方法

问题概述

在基于Django和Docker的Web应用中使用django-widget-tweaks时,系统提示该包未安装,报错ModuleNotFoundError: No module named 'widget_tweaks'。

相关配置文件

Dockerfile

# Pull base image
FROM python:3.10.2-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN pip3 install django-widget-tweaks

# Copy project
COPY . .

settings.py

INSTALLED_APPS = [
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'openai',   
]

INSTALLED_APPS += [
    'widget_tweaks',
]

报错信息

Exception in thread django-main-thread:
project-web-1  | Traceback (most recent call last):
...
project-web-1  | ModuleNotFoundError: No module named 'widget_tweaks'

解决方法

  1. 统一pip命令,消除环境差异
    Docker镜像中pip和pip3可能指向不同Python环境,将Dockerfile里的RUN pip3 install django-widget-tweaks改为RUN pip install django-widget-tweaks,和前面安装requirements的命令保持一致。

  2. 将依赖写入requirements.txt(推荐)
    单独在Dockerfile安装包易导致环境不一致,把django-widget-tweaks添加到requirements.txt:

django-widget-tweaks==1.4.12  # 可替换为最新稳定版本

之后删除Dockerfile中的RUN pip3 install django-widget-tweaks,所有依赖统一通过pip install -r requirements.txt安装,便于维护。

  1. 重新构建镜像并启动容器
    修改完成后执行以下命令:
docker-compose down
docker-compose build --no-cache
docker-compose up

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 15:07:12