Import of python_2_unicode_compatible fails after upgrading Django
python_2_unicode_compatible import error after upgrading DjangoShort answer
Django 3.0 removed the leftover Python 2 helpers, including python_2_unicode_compatible and django.utils.six. Code that still imports them fails on any current Django. Delete the decorator from your models, switch the import to six if you must keep it, or upgrade the old third-party package.
Fix it fast
1Find out which cause you have
Find every place that still uses the removed helpers, in your code and in installed packages.
grep -rn "python_2_unicode_compatible\|django.utils.six" --include=*.py . python -m pip show django | head -n 22Match what you see, then jump to its fix
What does the error look like?
python_2_unicode_compatible import error after upgrading DjangoCode importing django.utils.six fails on Django 3.0 and later
Cause 1 of 2
Your own models still use the decorator
1Check
The Django 3.0 release notes list django.utils.encoding.python_2_unicode_compatible among the removed APIs, an alias of the one in six. On Python 3, __str__ already returns text, so the decorator does nothing useful.
grep -rn "python_2_unicode_compatible" --include=*.py .2Fix
Delete the import and the decorator line. Keep the __str__ method as it is.
# before
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Animal(models.Model):
def __str__(self):
return self.name
# after
class Animal(models.Model):
def __str__(self):
return self.name3Verify
Django starts without the import error.
python manage.py checkConfirmed by docs.djangoproject.com
Cause 2 of 2
An old third-party package imports django.utils.six
1Check
The release notes say django.utils.six was removed and that code should drop it or use the six package directly. A package that was written for Django 2 and never updated still imports it.
python -m pip list | grep -i six2Fix
Upgrade that package to a release that supports your Django version. If no such release exists, replace the package. Installing six alone does not fix an import of django.utils.six.
python -m pip install --upgrade the-package-name3Verify
Run the project checks.
python manage.py checkConfirmed by docs.djangoproject.com
How do I stop it coming back?
- Read the release notes of each Django version before upgrading and run the tests with python -Wa manage.py test.
- Pin dependencies in a lock file and upgrade them together with Django.
- Drop packages that are no longer maintained.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Servers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
pip install cryptography tries to compile and fails in a Docker build
fatal error: 'openssl/aes.h' file not found
cryptography ships prebuilt wheels.
3 causes, each with a fixServers
cv2.imshow raises 'not implemented' because the wrong OpenCV package is installed
cv2.imshow() works in one environment and fails in another
The headless OpenCV packages are built without any GUI code, so cv2.imshow cannot open a window.
2 causes, each with a fixServers
pip install Cython succeeded, but the shell cannot find cythonize
a pip warning that a script was installed to a directory outside PATH
pip put the cythonize script in a folder your shell does not search.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fix