Python import: from django import template: module object 'template' has no attribute 'loader'
Question: I'm developing a Django project and deploying it to OpenShift PaaS. At first
everything worked, but after some changes irrelevant to template system (I
added django-hosts) something broke there and a `"module object 'template' has
no attribute 'loader'"` error started to appear. What was even stranger, it
appeared only two times after each wsgi app restart, and on 3rd request
everything started to work. I went back to last commit before breakage, but
the problem persisted. I recreated project from scratch and reinstalled my
Django app, but it didn't go either; error started appearing every time, not
just with first 2 requests. `from django import template` really imports
template module object, but this object lacks about 5 attributes, including
`loader`, as compared to what expected.

Then I noticed that same thing happens if I try to run the same code from
Django shell locally. But it still works in my app's views.py with local
Django development server. And in used to work in OpenShift initially. I tried
replacing `from django import template` with `from django.template import
loader` and calling `loader` directly - and EVERYTHING WORKED

I think I don't understand something about Python import. What's the
difference between

    
    
    import a
    a.b
    

and

    
    
    from a import b
    b
    

?

Why can a.b in first example miss attributes b has in second one?


Answer: That happened because template is a package inside django, and loader is a
module, while you expected template to be a module and loader one of it's
attributes. It works as expected.

