32

I've models.py as follows,

from django.contrib.auth.models import User
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.timezone import now
@python_2_unicode_compatible
class Tag(models.Model):
 name = models.CharField(max_length=50, unique=True)
 class Meta:
 verbose_name = 'tag'
 verbose_name_plural = 'tags'
 ordering = ['name']
 def __str__(self):
 return self.name
............. and so on

When I ran python manage.py syncdb this is the error I got:

itman@itman:~/djangoApp/mysite$ python manage.py syncdb
Traceback (most recent call last): 
 File "manage.py", line 10, in <module>
 execute_from_command_line(sys.argv)
 File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
 utility.execute()
 File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
 self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
 self.execute(*args, **options.__dict__)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 231, in execute
 self.validate()
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 266, in validate
 num_errors = get_validation_errors(s, app)
 File "/usr/lib/python2.7/dist-packages/django/core/management/validation.py", line 30, in get_validation_errors
 for (app_name, error) in get_app_errors().items():
 File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 158, in get_app_errors
 self._populate()
 File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 67, in _populate
 self.load_app(app_name)
 File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 88, in load_app
 models = import_module('.models', app_name)
 File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
 __import__(name)
 File "/home/itman/djangoApp/mysite/bmark/models.py", line 4, in <module>
 from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name python_2_unicode_compatible

I don't know why the module is not imported. I'm using Python 2.7 and Django 1.4.

yetty
2,4962 gold badges20 silver badges22 bronze badges
asked Dec 23, 2013 at 10:39

15 Answers 15

39

For the latest Django 3.0.4 , and auditlog try

from six import python_2_unicode_compatible

instead of

from django.utils.six import python_2_unicode_compatible

if it is not install run the below code

pip install six
answered Mar 17, 2020 at 10:55
Sign up to request clarification or add additional context in comments.

2 Comments

Django 3 doesn't support python 2, what would you get out of using python_2_unicode_compatible anyways?
@goose how to make change in the package file? i.e python3.8/site-packages/django_messages/encoding?
16

try

from django.utils.six import python_2_unicode_compatible

instead of

from django.utils.encoding import python_2_unicode_compatible

this works well for me in Django 1.10.6

answered Mar 13, 2018 at 19:26

5 Comments

works fine with django 2. Thanks for the suggestion. from django.utils.encoding import python_2_unicode_compatible this also works with django 2x
Django 3.0 changed to 'from six import python_2_unicode_compatible'
six is not available in django 3.0.2, still facing the issue
@AbhishekSoni Django 3.0+ no longer supports Python 2 (docs.djangoproject.com/en/3.0/releases/3.0/…) so you can probably just remove the imports and decorators from your code and run your app under Python 3.
from django.utils import six, timezone ImportError: cannot import name 'six'
8

I faced the same issue when I upgraded the Django version 2.x to 3.x.

This issue, I faced due to auditlog library.

First, execute the below command

pip uninstall auditlog

then

pip install auditlog3
answered Sep 16, 2020 at 7:15

Comments

7

python_2_unicode_compatible feature has only been added in Django 1.5 version.

https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.encoding.python_2_unicode_compatible

kmario23
62.1k17 gold badges174 silver badges160 bronze badges
answered Dec 23, 2013 at 10:44

4 Comments

Thanks mate! I did a django upgrade using the following: sudo apt-get install python-setuptools ; sudo easy_install --upgrade django and that solved the problem
This answer doesn't actually answer the question, and I'm getting the same error with django 1.3. Upgrading to 1.6 breaks graphite, so . . . thanks but this really is not an answer.
$ python -c "import django; print(django.get_version())" $ 1.6.5 but i allso have cannot import name python_2_unicode_compatible
This does not solve the problem... Check out @Desperad0 answer! It worked for me.
6

For me the issue was django-jet package which is not compatible with django3 there is an issue on django-jet github apparently you need to use django-3-jet instead.

answered Sep 22, 2021 at 18:27

1 Comment

It seems that django-jet is no longer maintained so, for Django 3.0 or 4.0 I'm using django-jet-reboot instead.
5

I ran into this issue when I wanted to use Django for Graphite. Turns out I had Django 1.3 installed and my Graphite version was breaking with Django> 1.5, so installing the latest version of the 1.4 branch fixed the problem:

sudo pip install --upgrade 'Django<1.5'
answered May 13, 2014 at 10:26

2 Comments

it is not solved: python -c "import django; print(django.get_version())" $ 1.6.5 but i allso have cannot import name python_2_unicode_compatible
This fixed my problem, Was also trying to install Graphite
3

It's actually also present in the 1.4 series since 1.4.2. You should really be using the latest 1.4.X release (1.4.10 as of the time of this writing) as earlier versions have known security vulnerabilities.

answered Dec 23, 2013 at 15:46

Comments

2

I was having a same error while i was using the Djnago-multiselect app that was becuase the app was trying to run the following import

from django.utils.encoding import python_2_unicode_compatible

But in the newer version of django python_2_unicode_compatible is not in the encodings.py but rather in the six module and install if six is not there for you using

pip install six

and then go to the django.utils.encoding.py file and simply import python_2_unicode_compatible from six like that

from six import python_2_unicode_compatible
answered Jan 20, 2021 at 12:21

Comments

1

I have founded same problem :

from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible'

I have update python version to python3.8, and I worked for me.

answered Feb 24, 2021 at 7:31

Comments

0

There is an existing package that supports Django 3: auditlog3

You can install it via pip install auditlog3

answered Jan 24, 2021 at 23:09

Comments

0

I have this upgrading Django 1.9 (Python 2.7) to Django 3.2 (Python 3.9).

You can solve this with a bash one liner:

grep -ril "from django.utils.encoding import python_2_unicode_compatible" your_project_source_code | xargs sed -i 's@from django.utils.encoding import python_2_unicode_compatible@from django.utils.six import python_2_unicode_compatible@g'
answered Jun 26, 2021 at 17:13

Comments

0

I was using Django in another computer, but wanted to copy the project with the virtual environment, too. It didn't work, so I had to recreate the environment. I got this error because I missed out one package from many:

django-background-tasks

So I had to install this package, and it solved the error.

answered Jul 21, 2021 at 12:20

Comments

0

Step 1: Install six by command: pip install six

Step 2: Go to the file: your_venv/lib/python3.7/site-packages/django/utils/encoding.py

Step 3: Add this line to your file: from six import python_2_unicode_compatible

Eric Jin
3,9144 gold badges23 silver badges48 bronze badges
answered Aug 2, 2022 at 10:28

Comments

0

for me replacing from django.utils.encoding import python_2_unicode_compatible with from django.utils.six import python_2_unicode_compatible with installing below library works! pip install django-utils-six

answered Oct 17, 2023 at 12:42

Comments

0

In my case, I had to upgrade Django and it worked.

To upgrade: pip install --upgrade django-registration-redux

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.