Moon
Moon

Reputation: 4160

Getting error cannot import name 'six' from 'django.utils' when using Django 3.0.0 latest version

Currently I have upgraded version of Django 2.2 to 3.0 and suddenly getting error like below.

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

I have checked Traceback is like below.

Traceback (most recent call last):
  File "c:\Users\admin\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\admin\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\admin\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Python37\Lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Python37\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\production\myproject\erp_project\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "d:\production\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "d:\production\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "d:\production\myproject\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "d:\production\myproject\venv\lib\site-packages\django\apps\registry.py", line 92, in populate
    app_config = AppConfig.create(entry)
  File "d:\production\myproject\venv\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "d:\production\myproject\venv\lib\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 "d:\production\myproject\venv\lib\site-packages\post_office\__init__.py", line 3, in <module>
    from .backends import EmailBackend
  File "d:\production\myproject\venv\lib\site-packages\post_office\backends.py", line 6, in <module>
    from .settings import get_default_priority
  File "d:\production\myproject\venv\lib\site-packages\post_office\settings.py", line 101, in <module>
    context_field_class = import_attribute(CONTEXT_FIELD_CLASS)
  File "d:\production\myproject\venv\lib\site-packages\post_office\compat.py", line 45, in import_attribute
    module = importlib.import_module(module_name)
  File "d:\production\myproject\venv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "d:\production\myproject\venv\lib\site-packages\jsonfield\__init__.py", line 1, in <module>
    from .fields import JSONField, JSONCharField  # noqa
  File "d:\production\myproject\venv\lib\site-packages\jsonfield\fields.py", line 21, in <module>
    from .encoder import JSONEncoder
  File "d:\production\myproject\venv\lib\site-packages\jsonfield\encoder.py", line 2, in <module>
    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)

I have checked in folder Lib\site-packages\django\utils and not found and six.py file but still from Lib\site-packages\jsonfield\encode.py containing line from django.utils import six, timezone which trying to import six but not able to find.

Earlier version of django containing six.py file in folder Lib\site-packages\django\utils.

Any idea how to solve this ?

Upvotes: 14

Views: 14324

Answers (5)

Frank
Frank

Reputation: 2029

In my case it was django-haystac causing this error. It helped me to upgrade the pip package to the newest beta.

pip install django-haystack==3.0b2

Upvotes: 0

Desert Camel
Desert Camel

Reputation: 137

Short Answer in Django 3.0 just install six:

pip install six

And just use it like:

import six

Upvotes: 0

Debdipta Halder
Debdipta Halder

Reputation: 487

in order to use the six module you can install it directly using pip and then modify the django-jsonfield package accordingly . What I mean is find the files in the package where there is from django.utils import six and replace them with import six. Then it should be working. I faced the same issue when using djongo with django 3.0. I found the respective file and replaced it with the above suggestion. Please note that it is never recommended to do this if you are working on a production level or enterprise level project. I did it for my pet project.

Upvotes: 7

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

Short answer: you might want to abandon django-jsonfield.

Based on the traceback, you are using the django-jsonfield package [GitHub], and this is a known issue [GitHub-issue]. It depends on the django.utils.six module, but that module has been removed in .

At the moment, you thus can not use with , and since the last commit to this project is from October 2017, perhaps the project is not that "active" anymore, and it thus might take a very long time (or even never) get fixed. The successor of is ([GitHub]). It was made compatible with by a pull request in October (2019) [GitHub-pr].

Upvotes: 8

Julien Kieffer
Julien Kieffer

Reputation: 1145

A specified in Django 3.0 release note, django.utils.six is removed. In case you need it, it is advised to use pypi packages instead

In your case, jsonfield package might be replaced by native Django's JSON Field. Another solution would be to fork jsonfield package yourself to solve you issue, or to make a PR on project's repo'

Upvotes: 3

Related Questions