14

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 ?

Moon
  • 4,014
  • 3
  • 30
  • 66
  • 2
    It is your `jsonfield` package that needs a module in Django that can no longer be found. – Willem Van Onsem Dec 05 '19 at 08:42
  • Which DB are you using? – Julien Kieffer Dec 05 '19 at 08:43
  • As documented in the [release notes](https://docs.djangoproject.com/en/3.0/releases/3.0/#removed-private-python-2-compatibility-apis), "django.utils.six - Remove usage of this vendored library or switch to six." – Sayse Dec 05 '19 at 08:43
  • @WillemVanOnsem - What should I do to fix this ? Any idea because I have installed latest version of all packages . – Moon Dec 05 '19 at 08:46

5 Answers5

8

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].

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
7

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.

Debdipta Halder
  • 497
  • 1
  • 5
  • 19
  • This was the solution for me too! Thanks. I hope this not destroy my app in production evironment in Heroku. – Jose Luis Quichimbo Feb 18 '20 at 23:09
  • What if you are not using it to modify a third party app, but using it directly in your project? Are there still any draw backs? – Nnaobi Feb 10 '22 at 00:31
3

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'

Julien Kieffer
  • 1,116
  • 6
  • 16
0

Short Answer in Django 3.0 just install six:

pip install six

And just use it like:

import six
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Desert Camel
  • 127
  • 11
0

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
Frank
  • 1,959
  • 12
  • 27