Using mako.imports setting in Pyramid

152 views
Skip to first unread message

Hollister

unread,
Oct 7, 2012, 12:28:43 PM10/7/12
to mako-d...@googlegroups.com
I'd like to use  mako.imports = 'import logging' in the .ini file to replace

<%!
    import logging
%>

In each template, but it does not work. The template just throws NameError: name 'logging' is not defined when I attempt to use it.

It appears the mako.imports doesn't even get called, since a setting like mako.imports = 'import SpamAndEggs' doesn't throw an error.

This is probably a Pyramid issue, but I thought I'd see if anybody here. has an idea. I'm using Pyramid 1.3 and Mako 0.7.0.

I also posted this on SO, but not a peep yet.

Michael Bayer

unread,
Oct 7, 2012, 6:34:46 PM10/7/12
to mako-d...@googlegroups.com
i'm not sure what mako.imports is, that sounds like part of the Pyramid integration, so you'd want to check that as far as setting the config like this.

For sanity, here's how it looks in pure Mako libs:

>>> from mako.lookup import TemplateLookup
>>> l = TemplateLookup(imports=["import logging"])
>>> l.put_string("some template", "hello world")
>>> print l.get_template("some template").code

from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 8
_modified_time = 1349649241.423057
_enable_loop = True
_template_filename = None
_template_uri = 'some template'
_source_encoding = 'ascii'
import logging
_exports = []


def render_body(context,**pageargs):
    __M_caller = context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        __M_writer = context.writer()
        # SOURCE LINE 1
        __M_writer(u'hello world')
        return ''
    finally:
        context.caller_stack._pop_frame()




--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mako-discuss/-/LOip4Vchf1QJ.
To post to this group, send email to mako-d...@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.

Hollister

unread,
Oct 7, 2012, 8:37:51 PM10/7/12
to mako-d...@googlegroups.com
Ok, so looking at the Pyramid-generated Mako template code, it just literally outputs what's in that config setting, quotes, brackets and all.
So, the syntax has to be:

mako.imports = import logging

with multiple imports as

mako.imports = import logging, some.other.module

not

mako.imports = import logging, import some.other.module


Which I didn't infer from the documentation that reads "String list of Python statements, typically individual “import” lines" (but maybe I just didn't understand it correctly).

Anyway, many thanks for helping figure this one out, Mike!

Michael Bayer

unread,
Oct 7, 2012, 9:48:23 PM10/7/12
to mako-d...@googlegroups.com
seems like pyramid's doc is missing something.  The "string list" from the Mako pov is really a list of strings:

TemplateLookup(imports=["import logging", "import x", "import y"])

so maybe something has to happen in a pyramid config file to turn it into a list.    or maybe its just not supported.

To view this discussion on the web visit https://groups.google.com/d/msg/mako-discuss/-/elOoUhJwqaAJ.

Hollister

unread,
Oct 7, 2012, 10:08:56 PM10/7/12
to mako-d...@googlegroups.com
That's exactly what I thought. Nobody has replied from the Pyramid list yet (pylons-discuss).

Hollister

unread,
Oct 8, 2012, 7:42:03 AM10/8/12
to mako-d...@googlegroups.com
From pylons-discuss, the syntax is:

mako.imports = 
    import logging 
    import some.other.module 

Import statements separated by line breaks. I think the docs should state this with an example, as "string list" to Python people is rather different.
Reply all
Reply to author
Forward
0 new messages