Reputation: 3918
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.
Upvotes: 4
Views: 400
Reputation: 3918
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.
Upvotes: 2
Reputation: 3918
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
and not any of these:
mako.imports = 'import logging'
mako.imports = ['import logging', 'some.other.module']
mako.imports = import logging, import some.other.module
The last throws a mako.exceptions.SyntaxException
since it's not valid Python, but the first 2 fail silently, doing nothing, since they just output a literal as a line of code.
I didn't infer this from the documentation that reads "String list of Python statements, typically individual “import” lines" (but maybe I just didn't understand it correctly).
Thanks to Mike Bayer for his post on Mako.
Upvotes: 1