Opened 16 years ago

Closed 16 years ago

#7684 closed (invalid)

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

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

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 Phalip 16 years ago.
Simple code illustrating the problem

Download all attachments as: .zip

Change History (6)

by Julien Phalip, 16 years ago

Attachment: FakeProject.zip added

Simple code illustrating the problem

comment:1 by Brian Rosner, 16 years ago

Resolution: invalid
Status: newclosed

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 by Malcolm Tredinnick, 16 years ago

Component: UncategorizedInternationalization
Resolution: invalid
Status: closedreopened
Triage Stage: UnreviewedAccepted

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 by Malcolm Tredinnick, 16 years ago

Summary: Strange import problem since upgrading to newforms-adminugettext() in app `__init__.py` causes error.

(Changed title to reflect probable problem.)

comment:4 by Brian Rosner, 16 years ago

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 by Julien Phalip, 16 years ago

Resolution: invalid
Status: reopenedclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top