python_2_unicode_compatible error

Question:

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.

Asked By: kmario23

||

Answers:

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

Answered By: yetty

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 By: Tim Graham

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 By: mre

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 By: Desperad0

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 By: goose

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 By: Sathiamoorthy

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 By: Hassan Shahzad

There is an existing package that supports Django 3:
auditlog3

You can install it via pip install auditlog3

Answered By: Yevhenii Molodtsov

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 By: Pedrinux81

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 By: shakaran

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 By: Chris

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 By: Amir Heshmati

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

Answered By: SHAILENDRA UPADHYAY