pythondjangopython-2.7django-admindjango-guardian

django guardian importerror but other examples work


First off, I am new to both django and python.

I am working on an existing project that is using django-guardian 1.3.2. I verified the server this app is deployed is also using 1.3.2 via pip list.

I am trying to understand the cause of an exception.

There are existing admin.py files, which load just fine.

#!/usr/bin/env python
# coding: utf-8
from guardian.admin import GuardedModelAdmin
from django.contrib import admin

from devices import models


class SomeModelAdmin(GuardedModelAdmin):
  pass


admin.site.register(models.SomeModel, SomeModelAdmin)

I added mine, and this also works (on my machine).

#!/usr/bin/env python
# coding: utf-8
from guardian.admin import GuardedModelAdmin


from django.contrib import admin
from common import models


class MyModel1Admin(GuardedModelAdmin):
    pass


admin.site.register(models.MyModel1, MyModel1Admin)


class MyModel2Admin(GuardedModelAdmin):
    pass


admin.site.register(models.MyModel2, MyModel2Admin)

We deployed the app, and now it fails to start with the following exception:

Traceback (most recent call last): File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in call self.load_middleware() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware mw_instance = mw_class() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/middleware/locale.py", line 24, in init for url_pattern in get_resolver(None).url_patterns: File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/urlresolvers.py", line 365, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in urlconf_module self._urlconf_module = import_module(self.urlconf_name) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module import(name) File "./sites/zpanel/urls.py", line 19, in admin.autodiscover() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/contrib/admin/init.py", line 29, in autodiscover import_module('%s.admin' % app) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module import(name) File "./common/admin.py", line 3, in from guardian.admin import GuardedModelAdmin ImportError: No module named admin

The server is running python 2.7.6, and I am using 2.7.10. I wouldn't think this is an issue since other admin.py files import from guardina.admin just fine.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'sites.this_app',
    'notifications.email',
    'raven.contrib.django.raven_compat',
    'guardian',
    'my_stuff',
    'existing_ones'
    'rest_framework',
    'rest_framework.authtoken',
    'south',
    'djangular',
    'widget_tweaks',
    'djrill',
    'corsheaders',
    'django_extensions',
    'parler',
    'watson',
    'baldr',
)

Solution

  • As it turns out, there was a directory called guardian in the same directory as my admin.py file. This directory isn't in the source (anymore), nor is it on my local machine. I guess it was some residue from some deployment long ago...