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

Django项目中'allauth.accountusers'模块找不到错误的解决方法

Django + django-allauth 部署后迁移报错解决方案

问题场景

开发Docker部署的Django项目,添加django-allauth实现邮箱登录,执行以下操作后迁移数据库报错:

  1. 在运行的容器中执行docker-compose exec web pipenv install django-allauth==0.52.0安装依赖
  2. 执行docker-compose down和docker-compose up -d --build重启并重新构建镜像
  3. 运行docker-compose exec web python manage.py migrate时触发模块找不到错误

错误信息

Traceback (most recent call last):
File "/code/manage.py", line 22, in <module>
main()
File "/code/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 416, in execute
django.setup()
File "/usr/local/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/apps/config.py", line 178, in create
mod = import_module(mod_path)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'allauth.accountusers'

相关配置文件

settings.py 关键片段

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Third-party
    'crispy_forms', # new
    'allauth',    # new
    'allauth.account'  # new


    # Local
    'users.apps.UsersConfig',
    'pages.apps.PagesConfig',
]

# 其余配置省略...

Pipfile 内容

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "==4.2"
psycopg2-binary = "==2.9.5"
django-crispy-forms = "==2.0"
django-allauth = "==0.52.0"

[dev-packages]

[requires]
python_version = "3.11"

解决步骤

  1. 修复INSTALLED_APPS语法错误:在'allauth.account'末尾添加逗号,确保列表元素分隔正确,修改后片段如下:
# Third-party
    'crispy_forms', # new
    'allauth',    # new
    'allauth.account',  # new 此处添加逗号
  1. 重新构建并启动容器:
docker-compose down
docker-compose up -d --build
  1. 再次执行数据库迁移:
docker-compose exec web python manage.py migrate

错误原因

Python支持字符串隐式拼接,当'allauth.account'后未加逗号时,Python会忽略注释和空行,将其与下一行的'users.apps.UsersConfig'部分内容拼接,生成了不存在的模块名'allauth.accountusers',导致Django导入模块失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 06:16:04