Sarek
Sarek

Reputation: 250

django: no module found named <app>.urls

I am currently trying to develop a rather simple application using django, but I am stuck pretty much at the beginning: My project is named "kundencenter", my app is "customermgr".

Both, the project and the app have a urls.py, the project's urls.py includes the app's:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^customer/', include('kundencenter.customermgr.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

But when I now try to access customer/, I am presented with an ImportError:

Django Version: 1.4
Exception Type: ImportError
Exception Value:    
No module named customermgr.urls
Exception Location: /usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
    ['/usr/lib/python2.7',
     '/usr/lib/python2.7/plat-linux2',
     '/usr/lib/python2.7/lib-tk',
     '/usr/lib/python2.7/lib-old',
     '/usr/lib/python2.7/lib-dynload',
     '/usr/local/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/dist-packages/PIL',
     '/usr/lib/python2.7/dist-packages/gtk-2.0',
     '/usr/lib/pymodules/python2.7',
     '.',
     '/var/www/kundencenter',
     '/var/www']

As you can see, I've already gone nuts with the PYTHONPATH, but to no avail. As you might have guessed, the project is located in /var/www/kundencenter. I have also already checked that the __init__.py files were created, which they were (they are empty). The error appears when I run manage.py runserver and also using Apache with mod_wsgi.

I am pretty much at the end of my wisdom. Does anybody have any idea how I could get rid of this error?

Upvotes: 4

Views: 7258

Answers (4)

tatlar
tatlar

Reputation: 3176

You should be building everything under a virtual env using PIP, including installing Django and all the other modules/libraries you need. Then you don't have to worry about messing with the Python path. Trying Googling for Python+Django+Virtualenv+PIP and read some of the recommended setups. It really does simplify the whole installation and configuring process, plus everything is self-contained and you can deploy easily using Fabric or some other deployment tool.

Good luck and I hope that helps.

Upvotes: 2

Qrees
Qrees

Reputation: 36

Set your PYTHONPATH to:

['/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/var/www/kundencenter']

and urlpatterns to:

urlpatterns = patterns('',
    url(r'^customer/', include('customermgr.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I don't know what is your directory structure, so this is my best gess.

Upvotes: 0

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

You likely have a module/package installed somewhere else on your Python module search path of the same name that is being picked up first.

Add at the start of your WSGI script file:

import kundencenter
print kundencenter.__file__

and see where it gets picked up from.

Upvotes: 0

Zashas
Zashas

Reputation: 766

Have you tried without the project name ?

url(r'^customer/', include('customermgr.urls')),

instead of

url(r'^customer/', include('kundencenter.customermgr.urls')),

By the way, if it's not the case, import your models (or everything else) with the shortest path possible. If you import models inside the same app, juste use "from models import X,Y,Z".

If you have to import models, functions, classes, whatever, from any other app, use "from my_other_app.models import X,Y,Z", but never include your project name.

If someday, you want to recycle your app for another django project, it will spare you some pain :)

Upvotes: 2

Related Questions