ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation'

managers.py

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _



class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)

error message:

File "C:\Users\zesha\Documents\GitHub\sust-cse-alumni-repo\users\models.py", line 6, in <module>
    from .managers import CustomUserManager
  File "C:\Users\zesha\Documents\GitHub\sust-cse-alumni-repo\users\managers.py", line 2, in <module>
    from django.utils.translation import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation' (C:\Users\zesha\Documents\GitHub\sust-cse-al
umni-repo\iftu\lib\site-packages\django\utils\translation\__init__.py)

Help me fix this bug!

1 Like

Hi NobinPegasus, ugettext_lazy` was deprecated in v2.2 and no longer used in django v3+. Check your version of django’s documentation regarding lazy translation (here’s v3.2).

1 Like

Also here’s the relevant section in the Django 3.0 release notes, which tells you the names of the new functions: Django 3.0 release notes | Django documentation | Django

If you have a bunch of code using this function or similar that needs updating, check out my tool django-upgrade, which can do it automatically for you: GitHub - adamchainz/django-upgrade: Automatically upgrade your Django projects.

1 Like

I believe you are using Django 4. ugettext_lazy has been removed in Django 4.0. Please use gettext_lazy instead:

from django.utils.translation import gettext_lazy as _
21 Likes

How comes this issue is still in Django 4.0.2, should I do a PR for that?

What issue are you referring to?

the issue. Currently on each new Django 4.0.2 installation we need to:
open: /opt/homebrew/lib/python3.9/site-packages/jsonfield/fields.py
change the line 9 from:
from django.utils.translation import ugettext_lazy as _
to:
from django.utils.translation import gettext_lazy as _

in order to be able to launch Django

What is this package “jsonfield”?
From its path, it looks like it’s a third party package and not part of Django - in which case the issue needs to be addressed by that third party library and is not a Django issue.

you are right. Sorry for inconvenience I’ll bring it up there!

For all others searching for a solution. We fixed it by

  1. replacing the requirement django-jsonfield==1.4.1 with django-jsonfield-backport==1.0.5
  2. adding django-jsonfield-backport in settings to installed apps
  3. replacing the import in our model file from
    from jsonfield import JSONField to from django.db.models import JSONField
  4. rebuilding the migration files
2 Likes

Hi I used your tips and it worked,thank you :smiling_face_with_three_hearts:

1 Like

not working with me i install django 3.2 but same problem

Where do you find the location of the folder “site-packages”?

I looked at the directory for the file

File "/home/eric/.local/lib/python3.8/site-packages/django_celery_monitor/apps.py", line 5, in <module>
    from django.utils.translation import ugettext_lazy as _

and wasn’t sure where to locate the file apps.py. Would you like to provide a tutorial on where to locate the file?

Did you check for it at /home/eric/.local/lib/python3.8/site-packages/django_celery_monitor/apps.py?

Thanks for this answer, it actually worked

I am using Wagtail 6.0.1 and Django 5.0.3. The file I am using has this code has this code from django.utils.translation import gettext_lazy as _

However, I am still getting this error:

from django.utils.translation import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation' (/venv/lib/python3.12/site-packages/django/utils/translation/__init__.py).

the venv is generated from the docker image

FROM python:3.12.2-slim as builder-py

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

WORKDIR /code

ENV VIRTUAL_ENV=/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN apt update && apt install -y build-essential gcc zlib1g-dev libjpeg-dev && apt clean

RUN pip install -U pip

ADD ./requirements.txt /code/

RUN --mount=type=cache,target=/root/.cache pip install --no-compile -r /code/requirements.txt

FROM python:3.12.2-slim

ENV VIRTUAL_ENV=/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

COPY --from=builder-py /venv /venv
COPY --from=builder-py /code /code

RUN apt update && apt install -y git && apt clean

WORKDIR /code

EXPOSE 8000

COPY . /code/

CMD ["gunicorn", "-c", "/code/gunicorn.conf.py", "config.wsgi"]

LABEL VERSION v9.4.20
LABEL Description="Image for www.velda.com" Vendor=“Velda” Version="9.4.20"

ENV VERSION v9.4.20
ENV PYTHONPATH /code:$PYTHONPATH

This issue occurs because the ugettext_lazy alias was deprecated in Django 2.0 and completely removed in Django 3.0. This is probably a third-party app you are using trying to import ‘ugettext_lazy’

To solve that, put the following code in your settings.py file

# settings.py
import django.utils.translation as original_translation
from django.utils.translation import gettext_lazy

original_translation.ugettext_lazy = gettext_lazy