Jinja: Template imported/rendered twice

Created on 16 Dec 2014  ·  5Comments  ·  Source: pallets/jinja

I am just getting started with jinja2 and I ran into a really weird issue, also described on issue #255 (closed, but there was no followup by the reporter).

I'm on Windows 7 x64 running Python 3.4.1
I installed jinja2 with "easy_install Jinja2"

I have a ...jinja2-testjinja2-test.py containing this:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('jinja2-test', 'templates'))

template = env.get_template('mytemplate.html')

print(env.list_templates())
print(template.render(the='variables', go='here'))

and a template ...jinja2-testtemplatesmytemplate.html

<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the {{ the }} go {{ go }}
</body>

</html>

on the command line I do: python jinja2-test.py and the templates are listed and rendered twice:

...\jinja2-test> python jinja2-test.py
['mytemplate.html']
<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the variables go here
</body>

</html>
['mytemplate.html']
<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the variables go here
</body>

</html>

Most helpful comment

Had the same issue on arch linux with python 3.4.2

The PackageLoader constructor is running the whole script a second time, it's not 2 prints in the script itself.
Use FileSystemLoader instead and it works fine:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))

The documentation was a little misleading there ;)

All 5 comments

Looking at how ['mytemplate.html'] is printed twice, too, it looks a lot like your script contains two print statements of each type... I've tried it on both Linux and Windows and can't reproduce it.

If you can reproduce it, please create a full testcase showing the issue (and put it e.g. in a Git repo).

Had the same issue on arch linux with python 3.4.2

The PackageLoader constructor is running the whole script a second time, it's not 2 prints in the script itself.
Use FileSystemLoader instead and it works fine:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))

The documentation was a little misleading there ;)

I have the same issue on Ubuntu 14.04 with Python 2.7.11, Jinja2 2.8 and Flask 0.11.1.
And I am sure that my code runs only once!

Note: this happens only when I open some page first time after restart of the server! When I update page - templates are loaded once.

This is equivalent to creating a file example.py that imports itself:

data = "foo"

import example

print(data)

python example.py first imports example as __main__, then it imports itself as example. Because of the way the import cache works, this results in two executions of the module. That's just Python, it's not something Jinja can do anything about.

If you set up a real package with __init__.py and run it as a module, or as an entry point, this doesn't happen. Or put the main code in an if __name__ == "__main__": block.

Had the same issue on arch linux with python 3.4.2

The PackageLoader constructor is running the whole script a second time, it's not 2 prints in the script itself.
Use FileSystemLoader instead and it works fine:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))

The documentation was a little misleading there ;)

我也是这样解决的

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slisznia picture slisznia  ·  10Comments

Naddiseo picture Naddiseo  ·  9Comments

chuwy picture chuwy  ·  21Comments

FreddieChopin picture FreddieChopin  ·  9Comments

jeffwidman picture jeffwidman  ·  17Comments