gpt4 book ai didi

django - 导入错误 : cannot import name 'six' from 'django.utils'

转载 作者:行者123 更新时间:2023-12-03 14:04:15 31 4
gpt4 key购买 nike

最近,我从 升级了 Django 框架的版本。 2.0.6 3.0 突然调用python manage.py shell命令,我得到了这个异常:

ImportError: cannot import name 'six' from 'django.utils' (/path-to-project/project/venv/lib/python3.7/site-packages/django/utils/init.py)



完整跟踪:

Traceback (most recent call last):
File "manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/path-to-project/project/venv/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/__init__.py", line 1, in <module>
from .checks import check_settings # noqa: F401
File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in <module>
from django.utils import six

类似问题:

我读了 Question这个 , release note ,但这些资源无法帮助我。

最佳答案

为什么会出现此错误/异常?
来自 release notes ,

django.utils.six - Remove usage of this vendored library or switch to six.


表示, django.utils.six 模块已从 中删除 .
我的代码库没有使用“ django.utils.six”模块,那么为什么会出现这个错误?
由于两个原因,可能会引发此导入错误,
  • 最重要的是,您安装的任何软件包都在使用 django.utils.six模块
  • 或者你的代码库使用 django.utils.six 模块

  • 注意:大多数时候第一个原因是小人😖😖
    如何识别导致错误/异常的包?
    简单的方法是, 查看您最后几行错误回溯 ,它会告诉你哪个包导致了异常。
    例子
    排头
    在本例中, corsheaders 模块导致导入错误
      File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/__init__.py", line 1, in 
    from .checks import check_settings # noqa: F401
    File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in
    from django.utils import six
    示例 2
    在本例中, jsonfield 模块导致导入错误
      File "d:\production\myproject\venv\lib\site-packages\jsonfield\fields.py", line 21, in 
    from .encoder import JSONEncoder
    File "d:\production\myproject\venv\lib\site-packages\jsonfield\encoder.py", line 2, in
    from django.utils import six, timezone
    ImportError: cannot import name 'six' from 'django.utils' (d:\production\myproject\venv\lib\site-packages\django\utils\__init__.py)
    示例 3
    在此示例中 parler 模块导致导入错误
    ...
    File "/path/to/project/venv/lib/python3.8/site-packages/parler/utils/conf.py", line 10, in
    from django.utils import six
    ImportError: cannot import name 'six' from 'django.utils' (/path/to/project/venv/lib/python3.8/site-packages/django/utils/__init__.py)
    示例 4
    在此示例中 django_mysql 模块导致导入错误
      File "/home/jerin/.virtualenvs/webscraperio/lib/python3.6/site-packages/django_mysql/checks.py", line 9, in 
    from django_mysql.utils import collapse_spaces
    File "/home/jerin/.virtualenvs/webscraperio/lib/python3.6/site-packages/django_mysql/utils.py", line 17, in
    from django.utils import six
    ImportError: cannot import name 'six'
    解决办法是什么?
    如果由于某些第三方软件包引起的错误,如 django-cors-headers , django-jsonfield 将对应的包版本升级到最新版本 .如果您已经在使用最新版本,请向开发人员报告问题。
    如果由于您的代码库引发错误,请使用 six包而不是 django.utils.six 模块

    关于django - 导入错误 : cannot import name 'six' from 'django.utils' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59193514/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com