Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 461
Moon Avatar asked Dec 05 '19 08:12

Moon


2 Answers

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 django-3.0.

At the moment, you thus can not use django-3.0 with django-jsonfield, 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 django-jsonfield is jsonfield2 ([GitHub]). It was made compatible with django-3.0 by a pull request in October (2019) [GitHub-pr].

like image 193
Willem Van Onsem Avatar answered Oct 26 '22 08:10

Willem Van Onsem


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.

like image 7
Debdipta Halder Avatar answered Oct 26 '22 10:10

Debdipta Halder