3

My Django project's directory hierarchy looks like this:

+ pybsd
|---+ devices
    |---+ templates
    |---+ views
        |---+ interaction
            |---- __init__.py
            |---- geraete.py
            |---- geraetemodelle.py
            |---- geraetegruppen.py
        |---- __init__.py
        |---- ajax.py
        |---- html.py
        |---- misc.py
    |---- __init__.py
    |---- urls.py
|---- __init__.py
|---- urls.py

(Please excuse the German names. I preferred not to replace them here since it would add yet another possible error source when trying out the solutions you'll hopefully suggest and answering your questions.)

Every request to http://URL/devices/.* is dispatched to the urls.py file living in /devices:

# ...
from views import html, ajax, misc, interaction

urlpatterns = patterns('', 
    # ...
    (r'^ajax/update/(?P<table>[a-z_]+)$', ajax.update),
    (r'^ajax/delete/(?P<table>[a-z_]+)$', ajax.delete),
    (r'^ajax/select_options/(?P<table>[a-z_]+)$', ajax.select_options),

    (r'^interaction/geraete/info/(?P<geraet>\d+)$', interaction.geraete.info),
    (r'^interaction/geraete/delete/(?P<geraet>\d+)?$', interaction.geraete.delete),
    (r'^interaction/geraetemodelle/delete/(?P<geraetemodell>\d+)?$', interaction.geraetemodelle.delete),
    (r'^interaction/geraetegruppen/delete/(?P<geraetegruppe>\d+)?$', interaction.geraetegruppen.delete),
    # ...
)

All URL definitions work except for those referencing the interaction package. I'm constantly getting the following error:

File "/home/simon/projekte/pybsd/../pybsd/devices/urls.py", line 33, in `<module>`
  (r'^interaction/geraete/info/(?P<geraet>\d+)$', interaction.geraete.info),
AttributeError: 'module' object has no attribute 'geraete'

I double-checked that the __init__.py files don't contain anything.

Maybe you've already found the (Python- or Django-related?) mistake I made and am apparently unable to see. If not, read on. In any case, thanks for reading this long post!


Isolating the problem

1st test

It works if I provide the view functions as strings:

(r'^interaction/geraete/info/(?P<geraet>\d+)$', 'devices.views.interaction.geraete.info'),
(r'^interaction/geraete/delete/(?P<geraet>\d+)?$', 'devices.views.interaction.geraete.delete'),
(r'^interaction/geraetemodelle/delete/(?P<geraetemodell>\d+)?$', 'devices.views.interaction.geraetemodelle.delete'),
(r'^interaction/geraetegruppen/delete/(?P<geraetegruppe>\d+)?$', 'devices.views.interaction.geraetegruppen.delete'),

... or add yet another line to the imports:

from views.interaction import geraete, geraetemodelle, geraetegruppen

Using from views.interaction import *, however, doesn't work either and results in the same error message.


2nd test

I created a file test.py in /devices:

from views import interaction
print dir(interaction)

Output:

simon@bsd-simon:~/projekte/pybsd/devices$ python test.py
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']

Again, no sign of the modules I created in the interaction package (geraete.py, geraetemodelle.py, geraetegruppen.py).

Unlike in urls.py, trying from view.interaction import geraete, geraetegruppen, geraetemodelle in test.py results in ImportError: No module named view.interaction this time.


3rd test

I started the Django shell:

$ python manage.py shell
>>> import devices.views.interaction.geraete
>>> dir(devices.views.interaction.geraete)
['Abteilung', 'Auftrag', 'Auftragsvorlage', 'Geraet', 'Geraetegruppe', 'Geraetemodell', 'HttpResponse', 'HttpResponseBadRequest', 'HttpResponseRedirect', 'Raum', 'Standort', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'delete', 'info', 'models', 'move', 'render_to_response']
>>> 

$ python manage.py shell
>>> from devices.views.interaction import geraete
>>> dir(geraete)
['Abteilung', 'Auftrag', 'Auftragsvorlage', 'Geraet', 'Geraetegruppe', 'Geraetemodell', 'HttpResponse', 'HttpResponseBadRequest', 'HttpResponseRedirect', 'Raum', 'Standort', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'delete', 'info', 'models', 'move', 'render_to_response']
>>> 

$ python manage.py shell
>>> import devices.views.interaction
>>> devices.views.interaction.geraete
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'geraete'
>>> dir(devices.views.interaction)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
1
  • 1
    Please change the title a bit. This is no really that much about Django than about Python itself. Commented Apr 13, 2010 at 9:03

3 Answers 3

3

When modules live in packages, and you import the package, Python does not automatically import all the modules in the package. Something in your program needs to import the modules you want to use. That can either be your urls module:

import views.interaction.gaerete

or, if you want interaction.garaete to always be available when you import interaction, it can be interaction/__init__.py:

import gaerete
Sign up to request clarification or add additional context in comments.

Comments

0

When you say

import devices.views.interaction

and later

interaction.geraete

Python looks for geraete in the __init__.py module in the interaction package.

If you want to see this works, you may include import geraete explicitly in the __init__.py module.

Comments

0

You should explicitly import the submodules if they are not imported in the __init__.py file:

import interaction.geraete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.