Django: cannot import name

mrmclovin picture mrmclovin · Mar 8, 2011 · Viewed 30.7k times · Source

I've just encountered a very annoying problem while deploying new updates to my website. I have two seperate websites whereas one of them is a development version. Now when I want to apply my changes to the production it won't work because of error message:

File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py", line 99, in get_response
request.path_info)

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 249, in resolve for pattern in self.url_patterns:

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 278, in _get_url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/usr/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 273, in _get_urlconf_module
self._urlconf_module = import_module(self.urlconf_name)

File "/usr/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
import(name)

File "/srv/websites/spelutveckla_se/urls.py", line 21, in
(r'^account/login/$', LoginView.as_view()),

NameError: name 'LoginView' is not defined

It doesn't matter if I remove that app, view, class or module because it will complain about an other module instead. LoginView IS defined in an import statement. The files are just a fresh copy of the development files (except for settings.py) that is running on an another subdomain without any problem. I've checked the settings.py several times and made sure the correct settings are set (differs by a couple of directory paths). I've also checked that the apache2 www-data user has permission to access the files. i've also restarted apache a couple of times and re-copied the files over and over but nothing works.

I'm desperate and have no clue of what the problem might be...?

Here's what my urls.py looks like at the top:

from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic import DetailView, ListView, TemplateView
from django.contrib import admin
from project.models import Project
from project.views import Detail, EditView as EditProject, CreateProjectView, EditProjectLinksView, EditProjectFeedsView, EditProjectTagsView, EditMultimediaView, PostForumTopic, AudioFormset
from frontpage.views import FrontpageView
from userprofile.views import UserRegistrationView, UserRegistrationActivationView, LogoutView, UserProfileView, LoginView, EditProfileView, CreateUserProfileView
from registration.views import activate as UserActivatedView

Answer

Filip Dupanović picture Filip Dupanović · Mar 8, 2011

You most likely had a circular import in the module LoginView was defined in, i.e. when you were importing the views module that defined LoginView, some statement somehow in turn imported some other module that was still waiting to get fully interpreted.

Here's an example to give you a better idea:

# myapp.urls

from django.conf.urls.defaults import *
from myapp import views

urlpatterns = patterns('',
    # ...
)

# myapp.views

from django.core.urlresolvers import reverse
from django.views.generic.edit import CreateView

class SomeCreateView(CreateView):

    # BOOM!
    success_url = reverse('myapp:some-url')

Once myapp.views gets imported and the SomeCreateView type gets allocated to memory, reverse('myapp:some-url') will get executed and your myapp.urls will eventually be imported by Django, only that that won't ever be possible since myapp.urls will indefinitely wait for myapp.views to get imported.