Forums

"jinja2.exceptions.TemplateNotFound:" error

Hi, I know this error has been raised before, but the solutions outlined in previous posts do not seem to be fixing the problem.

I have a python script that runs a web scrape using beautiful soup, then uses jinja2 to create an HTML template for an automated email.

When I open the file in pythonanywhere and click 'run', everything works and the email sends. But when I try and run as a task I receive the following error:

jinja2.exceptions.TemplateNotFound: "/home/tomrayner/xxxx/templates/xxx.html"

Can anyone suggest what I might be doing wrong?

You're probably using relative paths without paying attention to your working directory. See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

The path is laid out as above, which is an absolute path I think: "/home/tomrayner/xxxx/templates/xxx.html"

Is there a template file at that location?

yes, the html file is in that location

This is the code:

results_filename = "results_xxxxxx.html" results_template = environment.get_template("/home/tomrayner/xxxxxx/templates/xxxxxx.html")

with open(results_filename, mode="w", encoding="utf-8") as results: results.write(results_template.render(context)) with open('results_xxxxxx.html', 'r', encoding='utf-8') as f: html_string = f.read()

Jinja's get_template does not take a path, it takes a template name and looks for the template using the loaders that you have configured: https://jinja.palletsprojects.com/en/3.0.x/api/#jinja2.Environment.get_template