Search code examples
pythontemplatesdjango-templatesjinja2

Why does the Jinja2 template throw jinja2.exceptions.TemplateNotFound error even though file is there?


I'm struggling with the Jinja2 templating system. Even though the error path is pointing exactly to the .jinja2 file it is throwing template not found error.

Here is the current structure of folder.

❯ tree
.
├── generate_template.py
└── templates
    ├── kustomization.jinja2
    ├── pwa.jinja2
    └── shopware.jinja2

Here is content of generate_template.py

import jinja2
import os

# Define instance values
values = {
    "instance_name": "test",
    "instance_currency": "usd",
    "instance_locale": "en-us",
    "patch": "2023.06.01",
}


# Create Jinja2 environment with the loader
template_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")
loader = jinja2.FileSystemLoader('templates')
env = jinja2.Environment(loader=loader)

# Iterate over template files in the template directory

for template_name in os.listdir(template_dir):
    template_path = os.path.join(template_dir, template_name)
    template = env.get_template(template_path)
    # Render the template with values and write to file
    file_path = os.path.join(template_dir, template_name[:-7] + ".yaml") # Remove the .jinja2 extension and add .yaml
    with open(file_path, "w", encoding="utf8") as f:
        f.write(template.render(values))

Still, when I'm trying to execute the script I'm getting template not found error even though it is pointing exactly to the template.

❯ python3 generate_template.py
Traceback (most recent call last):
  File "/Users/oskar/Desktop/test/generate_template.py", line 22, in <module>
    template = env.get_template(template_path)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jinja2/environment.py", line 1010, in get_template
    return self._load_template(name, globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jinja2/environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jinja2/loaders.py", line 126, in load
    source, filename, uptodate = self.get_source(environment, name)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jinja2/loaders.py", line 218, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: /Users/oskar/Desktop/test/templates/kustomization.jinja2

I tried to use absolute path instead of relative, debugging it, printing paths - nothing helped.


Solution

  • Jinja will automatically read the file from the templates folder, you don't need to specify the full path (see https://jinja.palletsprojects.com/en/3.0.x/api/).

    Changing the line template = env.get_template(template_path) to template = env.get_template(template_name) should be enough.