Forums

jinja2.exceptions.TemplateNotFound when running from outside the folder

hi i wrote a python script to get same data from a database calculate same statistics and then send them on a pdf via email using flask and weasyprint, the script runs fine if i run it on the console inside the script folder, however when i try to run it outside the folder i get an error:

python3 Relatorio/GeradorDeRelatoriosDiarios.py (does't work)

cd Relatorio python3 GeradorDeRelatoriosDiarios.py (works fine)

WARNING: There are known rendering problems with Cairo <= 1.14.0
WARNING: @font-face support needs Pango >= 1.38
Traceback (most recent call last):
  File "/home/Hepha/Relatorio/GeradorDeRelatoriosDiarios.py", line 172, in <module>
    rendered = render_template("relatorio.html",stats=Stats,Data=data, NomeDoCliente=Info['Cliente'])
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 133, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.6/dist-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: relatorio.html

You're using a relative path to refer to your templates folder, so it matters where you run it from because that is where the tempalaes path will be relative to. See http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

you're are right! thanks! i ended up using a bash file to cd into the folder && run the script.