29

I'm following the Django Tutorials, I'm at the end of part 3, at Decoupling the URLconfs, at http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03 and I'm getting a "No module named urls" error message.

When I change:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('mysite.polls.views',
    (r'^polls/$', 'index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
    (r'^admin/', include(admin.site.urls)),
)

to:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/', include('mysite.polls.urls')),
    (r'^admin/', include(admin.site.urls)),
)

I changed include('mysite.polls.urls')), to include(mysite.polls.urls)),, but it still didn't work.

How to solve this problem?

UPDATE 2: at mysite/polls/urls.py is

from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

UPDATE 4: the whole project is at

http://www.mediafire.com/?t1jvomjgjz1

13
  • 1
    You could try posting the error message to a pastebin site and then linking to it here. That could help a lot. Commented Mar 1, 2010 at 2:24
  • 1
    Also, can you post the contents of mysite/polls/urls.py? Commented Mar 1, 2010 at 2:33
  • the contents of mysite/polls/urls.py have just been posted Commented Mar 1, 2010 at 2:41
  • 1
    Just double-check that it's actually named urls.py. I've mis-typed things to many times to count and spent hours refactoring code before I thought to check the filename. Commented Mar 1, 2010 at 2:54
  • From all of the information you've provided so far, the import shouldn't be failing. Can you try importing from the shell? Run "manage.py shell", then once it is running, enter "from mysite.polls import urls". Did you get the same ImportError? Commented Mar 1, 2010 at 3:32

7 Answers 7

52

I had a similar problem in my project root ... django complained that it couldn't find the module mysite.urls.

Turns out my ROOT_URLCONF variable in settings.py, which was set up using the default values, was set incorrect. Instead of "mysite.urls", it should have been simply "urls"

I changed it, and voila, it worked.

4
  • Thank you very much. This was my exact problem as well. Could it have something to do with me using a different settings module setup? (settings/development.py, settings/production.py)
    – Patrick
    Commented Dec 16, 2011 at 1:00
  • I discovered this issue is from me following Zachary Voase's project structure. Since it requires using django-admin.py and not manage.py, my site isn't in sys.path, and I had added the project package to path and not its parent directory. Once I amended PYTHONPATH from /path/to/site/project to /path/to/site, I was able to use the default ROOT_URLCONF and just had to change DJANGO_SETTINGS_MODULE=settings.deployment to DJANGO_SETTINGS_MODULE=project.settings.deployment.
    – Patrick
    Commented Dec 16, 2011 at 23:19
  • Looks like ROOT_URLCONF is the sole place that contains the name of the root module, so if you rename your project then it will break.
    – Steve
    Commented Feb 12, 2012 at 13:44
  • 1
    For me it worked the other way round. So had to replace 'urls' with 'mysite.urls' and viola, it worked :)
    – bhaskarc
    Commented Aug 31, 2016 at 7:19
8

I can't re-produce the import error on my machine using your project files (Windows 7, Django 1.1.1, Python 2.6.4). Everything imported fine but the urls were not specified properly (like the tutorial shows). Fixing the code:

/mysite/urls.py:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/', include('mysite.polls.urls')),
    (r'^admin/', include(admin.site.urls)),
)

/mysite/polls/urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

Visit http://127.0.0.1:8000/polls/ - I received a TemplateDoesNotExist exception because the template file is missing.

I'm afraid my answer might be to reboot and try it again. ;)

4
  • If he's using WSGI or something.... Django likes to super-cache the heck out of everything... I ran into so many headaches with that when I first started developing with Django..... dev server saves a lot of pain.
    – mpen
    Commented Mar 1, 2010 at 6:53
  • @Mark - He's going through the tutorial so I can't imagine setting up WSGI to serve it. Plus his SERVER_SOFTWARE (screenshot 3) is set to "WSGIServer/0.1 Python/2.6.4", which matches what my machine serves when using the dev server. Commented Mar 1, 2010 at 16:14
  • I was just like you said, "the urls were not specified properly (like the tutorial shows)", they really weren't. Commented Mar 2, 2010 at 0:23
  • Oh I figured.... but I was just sayin' ;) I tried developing on a live server. Not good.
    – mpen
    Commented Mar 2, 2010 at 2:33
8

Is there an __init__.py inside mysite/polls/ directory?

8
  • 2
    Empty is fine, that just tells Python the directory is a package. Make sure there's an __init__.py in the parent mysite directory too Commented Mar 1, 2010 at 3:05
  • There is too and by the way it's empty too. Commented Mar 1, 2010 at 3:11
  • 2
    I'm going to assume there is one. If Delirium is following the Django tutorial, he probably used the djangoadmin.py/manage.py commands to create the site and app. They place the __init__.py in there by default. As far as I can tell, Delirium has had a working site, using the polls module, up until he tried decoupling the URLConf from the site and placing it in the app. This seems to indicate that there's an issue as to the placement of the urls.py file and how it's called. Commented Mar 1, 2010 at 3:14
  • I've a question and a suggestion (that fix the problem). Question: why can't you just use include('mysite.pools.urls') ? Suggestion: You can try to import mysite.pools.urls before, i.e "from mysite.pools import urls as pool_urls" and use "include(pool_urls)" Commented Mar 1, 2010 at 3:15
  • chromano, what is the difference if import mysite.polls.urls before? by the way, it's really polls. dictionary.reference.com/browse/polls Commented Mar 1, 2010 at 3:31
5

I also had a weird problem with "No module named mysite.urls". The admin site was down and the my whole site.

The solution, after a few hours of searching the web, was on my side : Django is caching some of the settings in a file that he knows from an environment variable.

I just closed my terminal in which i was doing the runnserver thing and opened a new one.

2
  • Thank you, I would never have thought of that! Commented Dec 26, 2018 at 14:01
  • Thanks! When running django via IIS: restarting the IIS server is the equivalent action for closing-opening the terminal
    – Joris
    Commented Apr 21, 2022 at 11:49
1

I did exactly the same thing. Python newbie mistake by reading ahead. I created a file call "polls.url" thinking it was some sort of special django template file.

I misunderstood the text: "Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls.urls file should now look like this: "

It should really read: "Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls/urls.py file should now look like this: "

1

Late to the party but I fixed my problem by correcting a typo inside settings.py INSTALLED_APPS. I had 'webbapp' instead of 'webapp' so you might want to check on that as well (for people still having the problem)

0

Like Ryan Bagwell, problem was (at least temporarily) solved by changing ROOT_URLCONF in myproject/myproject/settings.py. I had to add "myproject" to ROOT_URLCONF to have there this: "%s.myproject.urls" % PROJECT_APP

Not the answer you're looking for? Browse other questions tagged or ask your own question.