Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

ImportError: cannot import name 'six' from 'django.utils'

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)

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 this , release note , but those resources couldn't help me.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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)

The module causing the issue, in this case, was parler. I hope this helps any others who encounter this issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...