jinja2.exceptions.TemplateNotFound 的原因,即使只使用 jinja2

Cause of jinja2.exceptions.TemplateNotFound even when using just jinja2

事后问题的原因很明显,但我想在这里分享不太明显的原因。

当运行代码如

import jinja2

templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(loader=templateLoader,
                                 trim_blocks=True,
                                 lstrip_blocks=True)

htmlTemplateFile = 'file.jinja.html'
htmlTemplate = templateEnv.get_template(htmlTemplateFile)

如果你遇到这个问题:

Traceback (most recent call last):
    ...
  File "file.py", line xyz, in some_func
    htmlTemplate = templateEnv.get_template(htmlTemplateFile)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jinja2/environment.py", line 812, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jinja2/environment.py", line 774, in _load_template
    cache_key = self.loader.get_source(self, name)[1]
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jinja2/loaders.py", line 187, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: file.jinja.html

你可能会发现网上的讨论指出这个问题一定与 jinja2 与 flask, with GAE, with Pyramid, or with SQL 的交互有关,并且可能确实是你的模板不在 "template" 文件夹中, 但这个问题可能是由 jinja2os 模块的交互引起的。

罪魁祸首正在更改当前目录,例如,

import os
os.chdir(someDir)

如果在此点之后调用 templateEnv.get_template(...),jinja2 将在 "current" 目录中查找模板,即使该目录已更改。

由于模块 os 提供 os.chdir 而不是 os.pushdir/os.popdir,因此必须模拟后一对或完全避免使用 chdir。