Code

Opened 6 years ago

Closed 6 years ago

#7684 closed (invalid)

ugettext() in app `__init__.py` causes error.

Reported by: julien Owned by: nobody
Component: Internationalization Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

*Please note*: I know this may look like it belongs to the user mailing list, but in [1] we thought Trac would be a better place for it.

I'm having a strange import issue since I have upgraded my project to NFA. To illustrate the issue I've attached a ZIP with very simple code.

Basically, the project won't run (that is, when the server is restarted and tries to launch Django). I get the error: "ImportError: cannot import name MenuItem".

Now, what is very strange here, is that I can get rid of the error in two different ways:

1) Either by swapping the order of the apps in INSTALLED_APPS:

From:

INSTALLED_APPS = (
    'menu',
    'menu_extension',
)

To:

INSTALLED_APPS = (
    'menu_extension',
    'menu',
)

2) Or by Removing the ugettext call in menu.models:

From:

caption = models.CharField(_('Caption'), max_length=50)

To:

caption = models.CharField('Caption', max_length=50)

Weird! Does that example uncover a bug in Django or rather my misuse of Python and/or NFA?

I'm using newforms-admin-SVN-7871.

[1] http://groups.google.com/group/django-users/browse_thread/thread/9427e8e78b199441#

Attachments (1)

FakeProject.zip (2.6 KB) - added by julien 6 years ago.
Simple code illustrating the problem

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by julien

Simple code illustrating the problem

comment:1 Changed 6 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

I don't know the exact reason, but you should never use ugettext at module level, but rather ugettext_lazy. It does fix the problem. Probably due to some odd importing that it might impose by forcing the string translation at module loading time.

comment:2 Changed 6 years ago by mtredinnick

  • Component changed from Uncategorized to Internationalization
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Accepted

Reopening and assigning to i18n then (as a very low priority item). I want to understand this problem more if that's the problem. Fatal issues like this are something we should avoid, if possible (or document if not).

For anybody wanting to work on this (and it isn't blindingly urgent): what I'm interested in understanding is why using ugettext() is causing a failure. The only thing I can think of off the top of my head is having to do imports to search for locale directories or something, but that doesn't sound likely.

comment:3 Changed 6 years ago by mtredinnick

  • Summary changed from Strange import problem since upgrading to newforms-admin to ugettext() in app `__init__.py` causes error.

(Changed title to reflect probable problem.)

comment:4 Changed 6 years ago by brosner

Fair enough :) I wonder if the app's __init__.py import of the local admin module might be causing this to go around like this. That convention is going away very soon with the fix of #6003.

comment:5 Changed 6 years ago by julien

  • Resolution set to invalid
  • Status changed from reopened to closed

As noted by brosner, I was having that issue because the admin module was imported in the init.py
Using the new recommended practice (e.g. autodiscover) removes that issue, so I'm now closing this ticket. As also noted by brosner above, ugettext_lazy should be used instead of ugettext in models declarations.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.