Scott Driscoll
Scott Driscoll

Reputation: 2929

file not accessible: 'templates/test.html' using jinja2 templates in certain directories

A simple GAE application threw the following error on self.jinja2.render_template() on only one computer, but not on any others (both macs and pcs):

ERROR    2012-02-14 21:54:04,987 webapp2.py:1528] [Errno 13] file not accessible: 'templates/test.html'
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/Users/scott/svn/GAE_branches/sample_broken_app/handlers.py", line 21, in get
    self.render_response('test.html', **context)
  File "/Users/scott/svn/GAE_branches/sample_broken_app/handlers.py", line 14, in render_response
    rv = self.jinja2.render_template(_template, **context)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/loaders.py", line 165, in get_source
    f = open_if_exists(filename)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/utils.py", line 224, in open_if_exists
    return open(filename, mode)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 592, in __init__
    raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: 'templates/test.html'
ERROR    2012-02-14 21:54:04,991 wsgi.py:205] 
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 193, in Handle
    result = handler(self._environ, self._StartResponse)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/Users/scott/svn/GAE_branches/sample_broken_app/handlers.py", line 21, in get
    self.render_response('test.html', **context)
  File "/Users/scott/svn/GAE_branches/sample_broken_app/handlers.py", line 14, in render_response
    rv = self.jinja2.render_template(_template, **context)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/loaders.py", line 165, in get_source
    f = open_if_exists(filename)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2/jinja2/utils.py", line 224, in open_if_exists
    return open(filename, mode)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 592, in __init__
    raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: 'templates/test.html'
INFO     2012-02-14 21:54:05,006 dev_appserver.py:2884] "GET /test HTTP/1.1" 500 -

The app is just:

handlers.py:

import webapp2

from webapp2_extras import jinja2

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(app=self.app)

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)


class MyHandler(BaseHandler):
    def get(self):
        context = {'message': 'Hello, world!'}
        self.render_response('test.html', **context)

webapp2_config = {}
webapp2_config['webapp2_extras.sessions'] = {
    'secret_key': 'ef23fsdawe444',
}

application = webapp2.WSGIApplication([
    webapp2.Route(r'/test', handler=MyHandler, name='test'),

], debug=True, config=webapp2_config)

app.yaml:

application: sampleapp
version: 0-01
api_version: 1
runtime: python27
threadsafe: false

builtins:
- remote_api: on

handlers:
- url: .*
  script: handlers.application

libraries:
- name: jinja2
  version: 2.6

- name: webapp2
  version: 2.3

There's also a templates directory with test.html in it.

Now when I run the app from a different directory, it works fine.

This google python group post gave me a hint to try a different directory, but I have no idea what's wrong with the original, which ran versions of the code without jinja2 from webapp2_extras fine.

Version info: OS X 10.6.8, GoogleAppEngineLauncher: 1.6.2, Python: 2.7.2

I reinstalled everything, and set my PYTHONPATH to "" in my .bash_profile, but that didn't change anything.

Note: I did strip out a few of the non public directory names from the debug output, but they didn't have spaces or anything.

A few others had a similar error, but their fixes were for older versions. There seems to be something about os.path

Upvotes: 4

Views: 3254

Answers (2)

Colin Su
Colin Su

Reputation: 4609

root folder and any folder you set in app.yaml as static dir won't work with jinja2 FileSystemLoader

While you're deploying, the value of upload_dir will go into CDN instead of GAE instance, though you're currently in local development server, App Engine SDK prevent you load files from where you couldn't find on production.

Upvotes: 3

fiatjaf
fiatjaf

Reputation: 12158

I was getting this error of yours, but without using the jinja2 from webapp2_extras (I was creating a jinja_enviroment myself, like

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

). I solved it by ignoring that os.path.dirname(__file__) that is in the docs and just putting

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))

So I thought I could help, but tried to solve your problem and nothing came. But when you said

A few others had a similar error, but their fixes were for older versions. There seems to be something about os.path

what did you mean? Are you using os.path in some part of the code? Because os.path is problematic and webapp2_extras.jinja2 don't use it, it just creates the environment with 'templates' handwritten as the template directory. Check the source.

So, maybe there's a hint somewhere in this comment. Good luck.

Upvotes: 2

Related Questions