kmario23
kmario23

Reputation: 61455

python_2_unicode_compatible error

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.

Upvotes: 32

Views: 87115

Answers (15)

Mario R. Matundulo
Mario R. Matundulo

Reputation: 1

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

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

Upvotes: 0

Spring98
Spring98

Reputation: 365

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

Upvotes: 0

SHAILENDRA UPADHYAY
SHAILENDRA UPADHYAY

Reputation: 1

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

Upvotes: 0

Sathiamoorthy
Sathiamoorthy

Reputation: 11630

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

Upvotes: 8

Amir Heshmati
Amir Heshmati

Reputation: 648

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.

Upvotes: 6

Chris
Chris

Reputation: 41

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.

Upvotes: 0

shakaran
shakaran

Reputation: 11102

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'

Upvotes: 0

Pedrinux81
Pedrinux81

Reputation: 11

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.

Upvotes: 1

Yevhenii Molodtsov
Yevhenii Molodtsov

Reputation: 35

There is an existing package that supports Django 3: auditlog3

You can install it via pip install auditlog3

Upvotes: 0

Hassan Shahzad
Hassan Shahzad

Reputation: 52

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

Upvotes: 2

goose
goose

Reputation: 761

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

Upvotes: 39

Desperad0
Desperad0

Reputation: 171

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

Upvotes: 16

mre
mre

Reputation: 1883

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'

Upvotes: 5

Tim Graham
Tim Graham

Reputation: 1535

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.

Upvotes: 3

yetty
yetty

Reputation: 2476

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

Upvotes: 7

Related Questions