This resolves the path to the templates/ folder relative to the python
package (templates.py).
The FileSystemLoader was relative to current working directory.
After this, it doesn't matter what your current working directory is.
def env():
return jinja2.Environment(
# Load template files from ./templates/
- loader=jinja2.FileSystemLoader('templates'),
+ loader=jinja2.PackageLoader(
+ package_name='templates',
+ package_path='templates',
+ ),
# Auto-HTML-escape any html or xml files.
autoescape=jinja2.select_autoescape(['html', 'xml', 'html.j2', 'xml.j2']),
# Whitespace control