问题描述
最近把Django框架的版本从2.0.6
升级到3.0
,调用后突然python manage.py shell
命令,我得到这个异常:
Recently, I upgraded the version of Django framework from 2.0.6
to 3.0
and suddenly after calling python manage.py shell
command, I got this exception:
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
类似问题:
我读了这个问题 和这个 django-3.0,发行说明,但这些资源无济于事我.
I read this Question and this django-3.0, release note , but those resources couldn't help me.
推荐答案
Django 3.0.0 发行说明指定某些 私有 Python 2 兼容性 API 已删除.其中包括 django.utils.six
.
The Django 3.0.0 release notes specify that certain private Python 2 compatibility APIs were removed. Among those was django.utils.six
.
对于这个错误,@WillemVanOnsem 指出模块 corsheaders
正在引用这个模块.
For this error specifically, @WillemVanOnsem noted that the module corsheaders
was referencing this module.
对于遇到同样问题的其他人,查看堆栈跟踪最后一行的文件路径可以帮助识别有问题的模块.我见过的另一个例子是:
For others encountering this same thing, looking at the file path on the last line of the stack trace can help with identifying the problematic module. Another example of this I've seen is:
...
File "/path/to/project/venv/lib/python3.8/site-packages/parler/utils/conf.py", line 10, in <module>
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)
在这种情况下,导致问题的模块是 parler
.我希望这对遇到此问题的其他人有所帮助.
The module causing the issue, in this case, was parler
. I hope this helps any others who encounter this issue.
这篇关于导入错误:无法从“django.utils"导入名称“6"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!