问题
Currently, I have upgraded version of Django from 2.0.6 to 3.0 and suddenly after calling python manage.py shell
command got this error:
ImportError: cannot import name 'six' from 'django.utils' (/path-to-project/project/venv/lib/python3.7/site-packages/django/utils/init.py)
Full trace:
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
Similar Questions:
I read this Question and django-3.0, release note , but those answers couldn't help me.
回答1:
The Django 3.0.0 release notes specify that certain private Python 2 compatibility APIs were removed. Among those was django.utils.six
.
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 stacktrace 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)
The module causing the issue in this case was parler
. Hope this helps any others who encounter this issue.
回答2:
Why this error/exception?
From django-3.0release notes,
django.utils.six
- Remove usage of this vendored library or switch to six.
means, django.utils.six
module was removed from django-3.0 onwards.
My codebase isn't using "django.utils.six
" module, then why this error?
This import error could be raised because of two reasons,
- Most importantly, any of your installed packages are using the
django.utils.six
module - or maybe your codebase using the
django.utils.six
module
NOTE: Most of the time the first reason is the villain 😖😖
How can I identify which package is causing the error/exception?
The easy way is, look into your last few lines of error traceback, and it will tell you which package is causing the exceptions.
Examples
Example-1
In this example, corsheaders module caused the the import error
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
Example-2
In this example, jsonfield module caused the the import error
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)
Example-3
In this example parler module caused the import error
...
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)
Example-4
In this example django_mysql module caused the import error
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'
What is the solution?
If the error raised because of some third-party packages like django-cors-headers,django-jsonfield, etc upgrade the corresponding package versions to latest versions.
If the error raised because from your codebase, use six package instead of django.utils.six
module
回答3:
There are a number of libraries and add-ons to Django that use django.utils.six, which of course are now broken. The main one of concern is mysql-connector-python (8.0.18). The simple solution is to use the library external to Django, but the authors of these libraries will need to make their changes (or you could temporarily make the changes yourself....replace django.utils.six with six).
回答4:
As mentioned by Mohammad Masoumi, upgrading the packages will resolve the issue because corsheaders
is supporting Django 3.0 now.
pip install --upgrade django-cors-headers
I also upgraded djangorestframework
and drf_yasg
to avoid this ImportError.
回答5:
I had the same problem. My issue was that used pip install django_taggit==0.22.2
. I resolved that when I did 'pip install django_taggit==1.2.0' because it is latest version.
来源:https://stackoverflow.com/questions/59193514/importerror-cannot-import-name-six-from-django-utils