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

Django-admin创建项目失败求助:collections.Iterator属性异常

问题解决:Django 2.0.7 + Python 3.10 执行startproject报错

错误原因

AttributeError: module 'collections' has no attribute 'Iterator' 是版本兼容性问题导致:

  • Python 3.10 把 collections.Iterator 迁移到了 collections.abc.Iterator 模块下
  • Django 2.0.7 是老旧版本,发布时 Python 3.10 还未推出,代码仍使用旧的导入路径,因此找不到该属性

解决方案

方案一:降级Python版本到兼容Django 2.0.7的版本

Django 2.0.7 官方支持的Python版本为3.4、3.5、3.6、3.7,推荐选Python 3.7:

  • 卸载当前Python 3.10
  • 安装Python 3.7版本
  • 重新创建虚拟环境:python -m venv Test1
  • 激活虚拟环境:D:\django-projects\Test1\Scripts\activate
  • 安装指定版本Django:pip install django==2.0.7
  • 再次执行项目创建命令:django-admin startproject trydjango1 .

方案二:升级Django版本到兼容Python 3.10的版本

如果不想更换Python版本,可升级Django到支持Python 3.10的版本(最低为Django 3.2):

  • 激活当前虚拟环境:D:\django-projects\Test1\Scripts\activate
  • 升级Django:pip install --upgrade django>=3.2
  • 执行项目创建命令:django-admin startproject trydjango1 .

错误回溯信息

Traceback (most recent call last):
File "C:\Users\TG\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in run_module_as_main
return run_code(code, main_globals, None,
File "C:\Users\TG\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "D:\django-projects\Test1\Scripts\django-admin.exe__main
.py", line 7, in
File "D:\django-projects\Test1\lib\site-packages\django\core\management_init
.py", line 371, in execute_from_command_line
utility.execute()
File "D:\django-projects\Test1\lib\site-packages\django\core\management_init_.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\django-projects\Test1\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\django-projects\Test1\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "D:\django-projects\Test1\lib\site-packages\django\core\management\commands\startproject.py", line 20, in handle
super().handle('project', project_name, target, **options)
File "D:\django-projects\Test1\lib\site-packages\django\core\management\templates.py", line 117, in handle
django.setup()
File "D:\django-projects\Test1\lib\site-packages\django_init_.py", line 16, in setup
from django.urls import set_script_prefix
File "D:\django-projects\Test1\lib\site-packages\django\urls_init_.py", line 1, in
from .base import (
File "D:\django-projects\Test1\lib\site-packages\django\urls\base.py", line 8, in
from .exceptions import NoReverseMatch, Resolver404
File "D:\django-projects\Test1\lib\site-packages\django\urls\exceptions.py", line 1, in
from django.http import Http404
File "D:\django-projects\Test1\lib\site-packages\django\http_init_.py", line 5, in
from django.http.response import (
File "D:\django-projects\Test1\lib\site-packages\django\http\response.py", line 13, in
from django.core.serializers.json import DjangoJSONEncoder
File "D:\django-projects\Test1\lib\site-packages\django\core\serializers_init_.py", line 23, in
from django.core.serializers.base import SerializerDoesNotExist
File "D:\django-projects\Test1\lib\site-packages\django\core\serializers\base.py", line 6, in
from django.db import models
File "D:\django-projects\Test1\lib\site-packages\django\db\models_init_.py", line 3, in
from django.db.models.aggregates import * # NOQA
File "D:\django-projects\Test1\lib\site-packages\django\db\models\aggregates.py", line 5, in
from django.db.models.expressions import Case, Func, Star, When
File "D:\django-projects\Test1\lib\site-packages\django\db\models\expressions.py", line 486, in
class TemporalSubtraction(CombinedExpression):
File "D:\django-projects\Test1\lib\site-packages\django\db\models\expressions.py", line 487, in TemporalSubtraction
output_field = fields.DurationField()
File "D:\django-projects\Test1\lib\site-packages\django\db\models\fields_init_.py", line 155, in init
if isinstance(choices, collections.Iterator):
AttributeError: module 'collections' has no attribute 'Iterator'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 18:48:23