Search code examples
pythonpyramidmako

Pyramid setting mako.imports not working


I'm trying to use the mako.imports setting to avoid importing modules in every template file.

Originally I had a module-level block at the top of every template like:

<%!
    import logging
%>

And I'd like to replace that with a setting in the .ini file:

mako.imports = 'import logging'

This does not work as 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.

I'm using Pyramid 1.3 and Mako 0.7.0.


Solution

  • 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.

    Thanks to Mike Merickel.