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!

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.

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 _
27 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

1 Like

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