Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshoot TemplateNotFound error from Flask under Gunicorn

I've got a Flask app that I'm trying to deploy using Gunicorn and nginx. However, although it works fine locally, it throws a TemplateNotFound error when I run in with Gunicorn on my remote server.

I'm not sure how to even start debugging this, let alone why it's failing...would love help on the former, if not the latter. I thought maybe it was a permissions issue, so chmod'd the templates folder to 777...no luck. Here's all the relavant details:

install script

Starting with a bare Ubuntu 10.04 install, I run this to set up the server and pull in my code: https://github.com/total-impact/total-impact-deploy/blob/master/deploy.sh. Then I put this nginx config file at /etc/nginx/sites-available/total-impact:

server {
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

Finally, I navigate the app directory and run gunicorn web:app, and hit the server's IP address. This generates a 500 in the browser, and this output on the command line:

stack trace:

root@jc:/home/ti/total-impact-webapp/totalimpactwebapp# gunicorn web:app2012-05-28 23:15:06 [15313] [INFO] Starting gunicorn 0.14.3
2012-05-28 23:15:06 [15313] [INFO] Listening at: http://127.0.0.1:8000 (15313)
2012-05-28 23:15:06 [15313] [INFO] Using worker: sync
2012-05-28 23:15:06 [15316] [INFO] Booting worker with pid: 15316
2012-05-28 23:15:12,274 - totalimpactwebapp.core - ERROR - Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1062, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1060, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1047, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ti/total-impact-webapp/totalimpactwebapp/web.py", line 60, in home
    return render_template('index.html', commits=False)
  File "/usr/local/lib/python2.6/dist-packages/flask/templating.py", line 120, in render_template
    return _render(ctx.app.jinja_env.get_template(template_name),
  File "/usr/local/lib/python2.6/dist-packages/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python2.6/dist-packages/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python2.6/dist-packages/jinja2/loaders.py", line 115, in load source, filename, uptodate = self.get_source(environment, name)
  File "/usr/local/lib/python2.6/dist-packages/flask/templating.py", line 61, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: index.html
like image 329
thisismyname Avatar asked May 29 '12 03:05

thisismyname


People also ask

How do you handle application errors in Flask?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.

How does Python handle 500 internal server error?

Make sure debug mode is off, then try again. Here is a comment directly from the code itself: Default exception handling that kicks in when an exception occurs that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged and the handler for a 500 internal server error is used.

How do you send an error response in Flask?

Sometimes when building a Flask application, you might want to raise a HTTPException to signal to the user that something is wrong with the request. Fortunately, Flask comes with a handy abort() function that aborts a request with a HTTP error from werkzeug as desired.


2 Answers

Today, I experienced identical problems after a long period of my Flask app behaving quite normally (ie not throwing TemplateNotFound exceptions). None of the approaches mentioned by others here hit the mark or seemed appropriate (eg app.debug, path manipulation).

Instead, I tracked it down to the standard Flask app initialisation line:

app = Flask(__name__)

I had changed __name__ to another value (to get access to a named logger), not expecting for all this carnage to unfold :-) Don't change this value unless you are very familiar with Flask internals.

like image 67
Alex Nunes Avatar answered Sep 26 '22 02:09

Alex Nunes


Are your templates in [app root]/templates/?

If so, check to be sure your path is correct. Put this as the first line in the view that handles your homepage:

return app.root_path

If that's what you expect to see - or if you're using Blueprints or another method that changes the default Jinja Environment somehow - it's a little more complicated.

Oddly, Jinja doesn't seem to have a jinja2.Environment.FileSystemLoader.get_search_path() method. I assumed it would have one :(

like image 38
Lyndsy Simon Avatar answered Sep 24 '22 02:09

Lyndsy Simon