Opened 18 years ago

Closed 18 years ago

Last modified 13 years ago

#3860 closed Uncategorized (invalid)

AttributeError: 'module' object has no attribute 'myapp'

Reported by: Vinay Sajip Owned by: Adrian Holovaty
Component: Contrib apps Version: dev
Severity: Normal Keywords:
Cc: dev@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I wanted to get access to the User model in django.contrib.auth.models from within my app. My app (called myapp) has the following __init__.py:

import django.contrib.auth
print django.contrib.auth #to show this gets imported OK
import django.contrib.auth.models

When I run manage.py, I get an error:

vinay@zeta-dapper:~/projects/mysite$ ./manage.py sql myapp
<module 'django.contrib.auth' from '/usr/lib/python2.4/site-packages/django/contrib/auth/__init__.pyc'>
Traceback (most recent call last):
  File "./manage.py", line 11, in ?
    execute_manager(settings)
  File "/usr/lib/python2.4/site-packages/django/core/management.py", line 1680, in execute_manager
    execute_from_command_line(action_mapping, argv)
  File "/usr/lib/python2.4/site-packages/django/core/management.py", line 1572, in execute_from_command_line
    translation.activate('en-us')
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 195, in activate
    _active[currentThread()] = translation(language)
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 184, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 167, in _fetch
    app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
  File "/home/vinay/projects/mysite/../mysite/myapp/__init__.py", line 3, in ?
    import django.contrib.auth.models
  File "/usr/lib/python2.4/site-packages/django/contrib/auth/models.py", line 4, in ?
    from django.contrib.contenttypes.models import ContentType
  File "/usr/lib/python2.4/site-packages/django/contrib/contenttypes/models.py", line 33, in ?
    class ContentType(models.Model):
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 30, in __new__
    new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 169, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/lib/python2.4/site-packages/django/db/models/options.py", line 53, in contribute_to_class
    setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', self.verbose_name + 's'))
  File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 42, in __wrapper__
    res = self.__func(*self.__args, **self.__kw)
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 255, in gettext
    _default = translation(settings.LANGUAGE_CODE)
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 184, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/lib/python2.4/site-packages/django/utils/translation/trans_real.py", line 167, in _fetch
    app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
AttributeError: 'module' object has no attribute 'myapp'

This doesn't seem right: why should I be able to import django.contrib.auth but not be able to import django.contrib.auth.models?

Change History (8)

comment:1 by anonymous, 18 years ago

Component: Core frameworkContrib apps

comment:2 by Simon G. <dev@…>, 18 years ago

Cc: dev@… added
Resolution: invalid
Status: newclosed
Summary: Unexpected error when trying to import django.contrib.auth.modelsAttributeError: 'module' object has no attribute 'myapp'

I'm unable to replicate this, so it's most likely a problem with myapp. However, we should probably work out some way of providing a better error message if possible!

Is everything in myapp configured properly? is django.contrib.auth in your INSTALLED_APPS setting? Can you ask this about this in the django-users mailing list?

comment:3 by Vinay Sajip <vinay_sajip@…>, 18 years ago

Resolution: invalid
Status: closedreopened

I replicated this as follows just after doing an svn update to get the latest files from the repository.

Ran django-admin.py startproject mysite.
From the mysite directory, ran ./manage.py startapp myapp.

Changed settings.py as per the following diffsettings output:

vinay@zeta-dapper:~/projects/mysite$ ./manage.py diffsettings
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/home/vinay/projects/mysite.db'
DEBUG = True
INSTALLED_APPS = ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'mysite.myapp']
LANGUAGE_CODE = 'en-gb'
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware')
ROOT_URLCONF = 'mysite.urls'  ###
SECRET_KEY = 'u_wf!1^!3y7iz!)=3cs8-=clsg^#g&2y16cavha2!fca3macrw'
SETTINGS_MODULE = 'mysite.settings'  ###
SITE_ID = 1  ###
TEMPLATE_DEBUG = True
TIME_ZONE = 'Europe/London'

I then modified myapp/__init__.py to add the following lines:

import django.contrib.auth
print django.contrib.auth
#import django.contrib.auth.models

and ran diffsettings again:

vinay@zeta-dapper:~/projects/mysite$ ./manage.py diffsettings
<module 'django.contrib.auth' from '/usr/lib/python2.4/site-packages/django/contrib/auth/__init__.pyc'>
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/home/vinay/projects/mysite.db'
DEBUG = True
INSTALLED_APPS = ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'mysite.myapp']
LANGUAGE_CODE = 'en-gb'
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware')
ROOT_URLCONF = 'mysite.urls'  ###
SECRET_KEY = 'u_wf!1^!3y7iz!)=3cs8-=clsg^#g&2y16cavha2!fca3macrw'
SETTINGS_MODULE = 'mysite.settings'  ###
SITE_ID = 1  ###
TEMPLATE_DEBUG = True
TIME_ZONE = 'Europe/London'

Notice that this produced the same output, with the additional print of the django.contrib.auth module at the top.

Now, removing the comment from the last line of myapp/__init.py___ and running diffsettings again produces the error.

I'll reopen the ticket this once, and if you close it again I'll take things up again on django-users; hope that's OK with you. However, I don't see that it's a problem with myapp or configuration, since I followed the steps in the tutorial.

comment:4 by Vinay Sajip <vinay_sajip@…>, 18 years ago

I am unable to attach a .tar.gz file with the site, as I get a Trac internal error (both here and on the Trac site, when I tried to report the error).

comment:5 by James Bennett, 18 years ago

Resolution: invalid
Status: reopenedclosed

Maybe I'm missing something here, but why are you importing things in __init__.py?

Also, this feels like a question that's more appropriate for the django-users mailing list. I'm going to mark as invalid and ask that discussion move there; if there's an actual bug we can revisit it in Trac as needed.

comment:6 by philippe.raoult@…, 18 years ago

It seems to be related to the way django loads apps. I had the same issue when playing around with includes in the contrib.admin module. Maybe we could catch this error and pretty print it (cannot load this module/function from init or something like that).

This has been reported in #2754 in another form. It's not really a bug but a rather an undocumented quirk.

comment:7 by eternicode, 14 years ago

Severity: Normal
Type: Uncategorized

Sorry to comment on a 4-year-old thread, but this is the first google result for this issue, no apparent resolution was reached here, and the django-users discussion (if there was one) is not linked :)

The problem is that myapp/__init__.py imports auth.models when it's executed; at this point, the subpackage myapp of mysite has not yet been imported, and is not yet an attribute on the parent package (mysite.myapp doesn't exist yet). auth.models, though, imports and invokes the translation framework, which in turn tries to import each of INSTALLED_APPS by importing the app's parent package (django.contrib or mysite) and doing a getattr for the app package -- ie, effectively getattr(mysite, 'myapp'). It's some kind of circular dependency breakdown.

The solution is 1) not import things that use the translation framework from an app's __init__.py or 2) delay the import until after all apps have been imported.

comment:8 by pllee, 13 years ago

Easy pickings: unset
UI/UX: unset

I noticed this error when I tried loading my admin page after a bunch of different commits. It turns out that I removed a function that one of my urls called, even though it was never called in my front-end or back-end code. I hope this helps someone out.

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