python_2_unicode_compatible error

67,831

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Solution 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'
Share:
67,831

Related videos on Youtube

kmario23
Author by

kmario23

Journeyman in Python, NumPy/CuPy/SciPy, C/C++, Fortran, (Multi) Linear Algebra, Computer Vision, Multimodal Learning, Deep Learning, PyTorch, Tensors, TensorFlow, Matplotlib, LaTeX, and Vim. Above all, I see Gaussians everywhere!!! Übung macht den Meister                                     Nice Reads: A delightful NumPy guide for Scientific Computing Python PEP A fast-paced primer on Matplotlib Git Ready Hangouts: Python-Chatroom, C++Chatroom Support: If you're a kind Samaritan and want to support me, please do so, for which I would eternally be thankful! paypal.me/kmario23

Updated on July 19, 2022

Comments

  • kmario23
    kmario23 almost 2 years

    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.

  • kmario23
    kmario23 over 10 years
    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
  • dannyman
    dannyman about 10 years
    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.
  • Kozlov V
    Kozlov V almost 10 years
    $ python -c "import django; print(django.get_version())" $ 1.6.5 but i allso have cannot import name python_2_unicode_compatible
  • Kozlov V
    Kozlov V almost 10 years
    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
  • zzarbi
    zzarbi almost 10 years
    This fixed my problem, Was also trying to install Graphite
  • Doogle
    Doogle almost 6 years
    works fine with django 2. Thanks for the suggestion. from django.utils.encoding import python_2_unicode_compatible this also works with django 2x
  • Ashen One
    Ashen One over 4 years
    Django 3.0 changed to 'from six import python_2_unicode_compatible'
  • VoidMain
    VoidMain over 4 years
    This does not solve the problem... Check out @Desperad0 answer! It worked for me.
  • Abhishek Soni
    Abhishek Soni about 4 years
    six is not available in django 3.0.2, still facing the issue
  • Brendan Quinn
    Brendan Quinn about 4 years
    @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.
  • Aarif
    Aarif almost 4 years
    Django 3 doesn't support python 2, what would you get out of using python_2_unicode_compatible anyways?
  • Muhammad Zeeshan
    Muhammad Zeeshan over 3 years
    @goose how to make change in the package file? i.e python3.8/site-packages/django_messages/encoding?
  • surya raj
    surya raj over 2 years
    from django.utils import six, timezone ImportError: cannot import name 'six'