ImportError: cannot import name 'six' from 'django.utils'
Asked Answered
C

17

155

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.

Critique answered 5/12, 2019 at 10:50 Comment(7)
You use a package corsheaders that still uses a module that was removed.Counts
@MohammadMasoumi Basically, remove explicit statements like from django.utils import six if you have them in your code, and then systematically bump the versions of all packages in requirements.txt that complain about this. In my case I had to also bump django-nested-admin and djangorestframework.Curler
If you are working with a package that hasn't been updated to work with django 3.0, you can fix this with a simple patch.Currycomb
Hi Mohammad, Does your problem being solved? What is the proper solution?Easy
Hi @MostafaGhadimi, as @WillemVanOnsem mentioned, I solved it by upgrading the corsheaders package. I appreciate you for sharing your experience as an answer here, I hope it'll solve someone's problem.Critique
check for install six and django-compress last versionAlan
My issue was with django-tag-parser, make sure you install the latest version. I have installed django-tag-parser==3.2 and the issue was resolved.Gauguin
P
66

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.

Pavlov answered 9/12, 2019 at 16:22 Comment(6)
Please install lower Django version, in my case I installed Django-2.1.4.Jeneejenei
@smartworld-dm Downgrading Django version will solve the issue, but you can't stay in older versions always, Change Is InevitableDeen
@ArakkalAbu Actually my project was using Django-2.1.4 and Django 3.0.0 was accidentaly installed.Jeneejenei
Lower Django solve mi problem. I will wait until the modules update their versions.Band
You also solve this upgrading the django-cors-headers package pip install --upgrade django-cors-headers as sugested by @aryan-jadon on Github.Tunicate
If there are any packages that are still using six then this is the best work around I have found Install this library: django-utils-six 2.0 for Django >= 3. pip install django-utils-sixPlayground
D
98

Why this error/exception?

From release notes,

django.utils.six - Remove usage of this vendored library or switch to six.

means, django.utils.six module was removed from onwards.

My codebase isn't using "django.utils.six" module, then why this error?

This import error could be raised because of two reasons,

  1. Most importantly, any of your installed packages are using the django.utils.six module
  2. 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

Corsheaders

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 you are already using the latest version, report an issue with the developer.

If the error raised because from your codebase, use six package instead of django.utils.six module

Deen answered 20/12, 2019 at 5:7 Comment(1)
One simple solution is mentioned here: #65067603Familial
F
72

Install this library: django-utils-six 2.0 for Django >= 3.

pip install django-utils-six
Fawcette answered 4/9, 2020 at 18:0 Comment(0)
P
66

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.

Pavlov answered 9/12, 2019 at 16:22 Comment(6)
Please install lower Django version, in my case I installed Django-2.1.4.Jeneejenei
@smartworld-dm Downgrading Django version will solve the issue, but you can't stay in older versions always, Change Is InevitableDeen
@ArakkalAbu Actually my project was using Django-2.1.4 and Django 3.0.0 was accidentaly installed.Jeneejenei
Lower Django solve mi problem. I will wait until the modules update their versions.Band
You also solve this upgrading the django-cors-headers package pip install --upgrade django-cors-headers as sugested by @aryan-jadon on Github.Tunicate
If there are any packages that are still using six then this is the best work around I have found Install this library: django-utils-six 2.0 for Django >= 3. pip install django-utils-sixPlayground
W
31

First, install six from pip

pip install six

Second, call six

from six import text_type

For me works, I have Django 3.0.4

Wrongful answered 22/3, 2020 at 10:49 Comment(1)
This worked for me as I was using six in my code.Joanajoane
H
24

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.

Hornswoggle answered 12/12, 2019 at 10:44 Comment(0)
G
13

You need to update the cors headers package:

pip3 install six
pip3 install --upgrade django-cors-headers 
Glycerol answered 21/2, 2020 at 2:52 Comment(2)
just downgrading to django==2.2 and just installing six solved the issue for me. Why do we need to upgrade cors-headers ?Acquisitive
If you upgraded your django version to 3.x you should consider upgrading cors-headers. That is another alternative also.Glycerol
D
10

Exception:

 File "/usr/local/lib/python3.8/dist-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.8/dist-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/dist-packages/django_celery_beat/models.py", line 6, in <module>
    import timezone_field
  File "/usr/local/lib/python3.8/dist-packages/timezone_field/__init__.py", line 1, in <module>
    from timezone_field.fields import TimeZoneField
  File "/usr/local/lib/python3.8/dist-packages/timezone_field/fields.py", line 5, in <module>
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (/usr/local/lib/python3.8/dist-packages/django/utils/__init__.py)

Solution:

 vi /usr/local/lib/python3.8/dist-packages/timezone_field/fields.py

Change:

from django.utils import six

To:

import six
Doley answered 11/2, 2020 at 20:42 Comment(1)
Modifying installed packages like this isn't a good idea. If you install the same packages on a new server then you'll get an error again. In this case, a better fix is to upgrade django-timezone-field - version 4.0 added support for Django 3.0.Basie
B
6

I had the same problem.

My issue was using:

pip install django_taggit==0.22.2

I resolved this when I did:

pip install django_taggit==1.2.0

because that is the latest version.

Benn answered 18/12, 2019 at 21:23 Comment(1)
This answer could be useful, but it's better as a comment under the original post since other people have already recommended upgrading other packages.Nickelous
P
6

Folks' ideal solution is an upgrade and clean usage, but a workaround for folks in dire straits is simple enough.

In Django utils create a new file six.py and inside the file put:

import six

NOTE: Not a solution but a workaround for immediate patching

Parabolic answered 25/2, 2020 at 6:27 Comment(0)
E
5

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

Erinerina answered 9/12, 2019 at 21:8 Comment(0)
J
5

I resolved this issue by installing a higher version of corsheader package.

corsheader v3.3.0 supports Django 3.0.x

django-cors-headers==3.3.0
Jeneejenei answered 27/5, 2020 at 11:59 Comment(0)
E
3

JSONField Solution:

I used jsonfield and jsonfiled2 packages. But in both cases, I faced the same error.

My problem solved when I have installed django-jsonfield package and uninstall the rest of the packages (related to jsonfield).

# In case you have installed the following packages, otherwise ignore them.
pip uninstall jsonfield
pip uninstall jsonfield2

pip install django-jsonfield

Usage:

from django.db import models
from jsonfield import JSONField

class ModelName(models.Model):
    json_field = JSONField()
Easy answered 29/1, 2020 at 16:59 Comment(0)
T
2

I had the exact same problem. Let me tell you how I solved it (fortunately it was simple to do).

So, what's going on?

You have to pay attention to the traceback Django is telling you (pro tip: start from the bottom):

  File "/path-to-project/project/venv/lib/python3.7/site-packages/corsheaders/checks.py", line 7, in <module>
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (/path-to-project/project/venv/lib/python3.7/site-packages/django/utils/init.py)

It tells you two important things:

  1. What's going on: ImportError: cannot import name 'six' from 'django.utils'
  2. Where it's happening: /lib/python3.7/site-packages/corsheaders/checks.py", line 7, in <module>

It is first telling you it cannot import the six module from django.utils, which is quite logical since Django has deprecated the module in Django 3.0.

Now you may ask:

  • but hey, I wasn't using that module!!
  • You're right, but a dependency was :)
  • Which dependency?
  • I'm glad you asked...

This one ==> /lib/python3.7/site-packages/corsheaders/checks.py, corsheaders was importing the module here: from django.utils import six in checks.py (in line 7).

What's the solution?

This issue is generally solved by updating the package that created the problem in the first place. They probably removed that import and replaced it with something else, if necessary. Go to the command line and type:

pip install corsheaders -U

What generated the problem?

Django stopped supporting Python 2. Since django.utils.six provided "Utilities for writing code that runs on Python 2 and 3", it was not longer necessary to support this module, so it was deprecated in Django 3.

Teacake answered 17/11, 2020 at 4:19 Comment(0)
S
2

Django six is not available for Django versions higher than django2, so a quick fix is to install it via pip:

pip install django-utils-six

Then you should be good to go

Sices answered 27/4, 2021 at 14:17 Comment(0)
C
1

If the error raised because of some third-party packages and you are already using the latest version, report an issue with the developer. If you did that but you really need a urgent fix, this strategy could make the third party package happy:

    
try:
    # six removed since Django 3.0
    from django.utils import six
except ImportError:
    import six
    import sys
    sys.modules["django.utils.six"] = six
    # similarly for any other six sub-module required:
    sys.modules["django.utils.six.moves"] = six.moves


# finally, import the outdated third-party package below:

import outdated_library

Calistacalisthenics answered 26/1, 2022 at 16:44 Comment(0)
M
0

As I understand, You just need to delete tokens.py file at all if You have a greater version of Django. And remove all imports from .tokens in other files such as views.py as well.

Mesquite answered 2/9, 2020 at 13:22 Comment(0)
D
0

FYI I got this same error from using the package django-braces. I had used Conda to install it: conda install -c conda-forge django-braces. By default as of the time of this answer (Jan 24, 2023) conda-forge installs version 1.13 of django-braces, which gives rise to the "cannot import 'six' from django.utils" error.

To fix it, I did the following:

conda remove django-braces
pip install django-braces

This removes django-braces v1.13 and replaces it with v1.15, which solves the problem. Happy django-ing everyone.

Dionedionis answered 25/1, 2023 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.