user17794736
user17794736

Reputation:

ImportError : cannot import name 'ugettext_lazy'

I'm trying to install suit, I entered this command : pip install https://github.com/darklow/django-suit/tarball/v2 and wrote this code :

from suit.apps import DjangoSuitConfig
class SuitConfig(DjangoSuitConfig) :
    layout = 'horizontal'

And added it :

INSTALLED_APPS = [
    'products.apps.SuitConfig',
     .....]

But when I added this last code I had this error :

InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'suit.templatetags.suit_menu': cannot import name 'ugettext_lazy' from 'django.utils.translation' (C:\Users\hp\environments\env3\lib\site-packages\django\utils\translation_init_.py)

note : django 4.01

Upvotes: 40

Views: 63202

Answers (10)

nour-logstic
nour-logstic

Reputation: 1

Use this as solution

pip install django-admin-honeypot-updated-2021

Upvotes: 0

someone
someone

Reputation: 1

Error: Django ImportError: cannot import name 'ugettext' from 'django.utils.translation'

This issue occurs because the ugettext alias was deprecated in Django 2.0 and completely removed in Django 3.0. The error message suggests changing the import statement to use gettext directly.

To solve this problem, you can swap the module to override the import.

Inside the settings.py file, add the following code:

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

original_translation.ugettext = original_translation.gettext

This workaround ensures that the ugettext attribute is replaced with the gettext attribute at the beginning of your Django application's execution.

Upvotes: 0

Shahram
Shahram

Reputation: 25

I fixed that by upgrade Django to 4.2.3, it worked perfectly.

Upvotes: 0

I strongly recommend to downgrade Django from "4.x.x" to "3.x.x" to solve your errors:

pip install django==3.*

Because Django 4.x.x is very new so some packages don't catch up with Django 4.x.x. So, if you keep using Django 4.x.x, you will get the same or similar errors in the near future then you will spend a lot of time to solve these errors because of Django 4.x.x.

Actually, I got the same or similar errors when using "django-graphql-jwt", "graphene-django" and so on. Then, for some packages, I could solve such errors but for some packages, I could solve but new other errors occurred then, I couldn't solve these new other errors.

Finally, I noticed I spent a lot of time to solve such errors because of Django 4.x.x. So again, I strongly recommend to downgrade Django from "4.x.x" to "3.x.x" to solve your errors:

pip install django==3.*

Upvotes: 2

Paul Velasco
Paul Velasco

Reputation: 1

You must install the django-admin-honeypot library like this:

pip install django-admin-honeypot-updated-2021

with that the problem is solved

Upvotes: -1

Muhammadalive
Muhammadalive

Reputation: 1086

This error belongs to Django versions. If you want to use Django 4.* you need to find:

from django.utils.translation import ugettext_lazy as _

and change to:

from django.utils.translation import gettext_lazy as _

Happy Coding :)

Upvotes: 49

Abhilash Meluveettil
Abhilash Meluveettil

Reputation: 181

If you are using django 4.* versions, then you will have to replace "ugettext_lazy" with "gettext_lazy" in import statements where ever applicable.

In my case I was getting error "unable to import 'ugettext_lazy'" even after this change. I had to upgrade "django-rest-passwordreset" from version 1.1.0 to 1.2.1 to fix the issue.

pip3 install django-rest-passwordreset==1.2.1.

Check for any dependent library similar to above which still uses the deprecated version and then upgrade it.

Upvotes: 2

Liang Wei
Liang Wei

Reputation: 163

I have solved the problem by installing the django==3.2 instead of the latest version of Django. But I`ll still check for the pypi update for the usage in the latest version of Django.

Upvotes: 0

Liang Wei
Liang Wei

Reputation: 163

I also got the same issue while using django-hitcount

views.py

from hitcount.views import HitCountDetailView

class PostDetailView(HitCountDetailView):
model = Post
template_name = 'blog/post.html'
slug_field = "slug"
count_hit = True

settings.py

INSTALLED_APPS = [
'hitcount',
 .....]

One answer is ugettext_lazy has been removed in Django 4.0 57. Please use gettext_lazy instead ""

from django.utils.translation import gettext_lazy as _

I put this line of code in the views.py, and got the same error.

Upvotes: 7

MiTriPy
MiTriPy

Reputation: 229

Was just about to ask the version but the note (django 4.01) helped. 'ugettext_lazy' seems to be used by the app you are trying to use as well.

'ugettext_lazy' has been deprecated for django 3+ so you won’t be able to use that with a django version >= 3. https://code.djangoproject.com/ticket/30165

Upvotes: 11

Related Questions