ImportError while started to use templates

257 views
Skip to first unread message

Brad Lucas

unread,
Nov 26, 2012, 12:00:51 AM11/26/12
to we...@googlegroups.com
Just starting with the simplest of examples to use templates I get the following error. Has anyone else seen this and can explain what is going on?

python app.py
Traceback (most recent call last):
  File "app.py", line 7, in <module>
    render = web.template.render('templates/')
  File "/Library/Python/2.6/site-packages/web/template.py", line 1028, in __init__
    self.mod = __import__(name, None, None, ['x'])
ImportError: No module named templates


My app.py looks like this:

import web

urls = (
    '/', 'index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')

class index:
    def GET(self):
        name = 'Bob'
        return render.index(name)

if __name__ == "__main__":
    app.run()



I have a sub-directory called templates with a file index.html in it.

The index.html file looks like:

$def with (name)

$if name:
    I just wanted to say <em>hello</em> to $name.
$else:
    <em>Hello</em>, world!




Thanks,

Brad


Brad Lucas

unread,
Nov 27, 2012, 12:05:55 PM11/27/12
to we...@googlegroups.com
By putting an empty __init__.py file inside of templates I have eliminated the ImportError. Can anyone explain why this is necessary? Any pointers to where I should have learned about this? Nothing I found mentioned needed this for a simple web.py example.

Next up though I get the following error:

<type 'exceptions.AttributeError'> at /

'module' object has no attribute 'index'

So it is not exactly fixed. There is an index.html file in templates yet the render.index call seems to fail.

Any ideas?

Brad Lucas

unread,
Nov 27, 2012, 12:41:18 PM11/27/12
to we...@googlegroups.com
The __ini__.py file made no sense so I removed it.

Back to Import Error: No module named templates

Now looking into the line 1028 of the templates.py file I notice.

The line ... in my file
render = web.template.render('templates/')

ends up with an error inside a class called GAE_Render

class GAE_Render(Render):
    # Render gets over-written. make a copy here.
    super = Render
    def __init__(self, loc, *a, **kw):
        GAE_Render.super.__init__(self, loc, *a, **kw)
        
        import types
        if isinstance(loc, types.ModuleType):
            self.mod = loc
        else:
            name = loc.rstrip('/').replace('/', '.')
            self.mod = __import__(name, None, None, ['x'])     # line 1028


Why do you think creating an render object is ending up inside of a Google App Engine class. 

I'm just running the simple example on the command line.


- Brad






On Monday, November 26, 2012 12:00:51 AM UTC-5, Brad Lucas wrote:

Brad Lucas

unread,
Nov 27, 2012, 1:05:46 PM11/27/12
to we...@googlegroups.com
OK. Looks like the template.py file has an internal check and if it can find an installed Google App Engine SDK then it uses GAE_Render instead of render.

By moving the SDK out of the way I was able to run the simple example.



- Brad

sni

unread,
Dec 7, 2012, 2:23:19 AM12/7/12
to we...@googlegroups.com
Hi Brad,

try:

render = web.template.render(os.path.join(os.path.dirname(__file__), 'templates/'))
Reply all
Reply to author
Forward
0 new messages