File not available: "templates / test.html" using jinja2 templates in specific directories

A simple GAE application ran the following error on self.jinja2.render_template () on only one computer, but not on the other (both mac 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 application is simple:

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 is also a template directory with test.html.

Now when I run the application from another directory, it works fine.

This google python group gave me a hint to try a different directory, but I don’t know what happened to the original, that running versions of the code without jinja2 from webapp2_extras fine.

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

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

Note. I removed several of the non-public directory names from the debug output, but they did not have spaces or anything else.

Several others had similar errors , but their fixes were for older versions. Something seems to be about os.path

+4
source share
2 answers

the root folder and any folder installed in app.yaml as a static directory will not work with jinja2 FileSystemLoader

During deployment, the upload_dir value will be sent to the CDN instead of the GAE instance, although you are currently on the local development server, the Engine application SDK does not allow you to download files from the place where you could not find it during production.

+3
source

I was getting this error, but without using jinja2 from webapp2_extras (I myself created jinja_enviroment, e.g.

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

) I solved this by ignoring this os.path.dirname(__file__) found in the docs and just putting

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

So I thought I could help, but I tried to solve your problem, and nothing worked. But when did you say

Some others had a similar error, but their fixes were earlier versions. Something seems to be about os.path

what did you mean? Are you using os.path in some part of the code? Since os.path is problematic and webapp2_extras.jinja2 does not use it, it simply creates an environment with "templates" written in manuscript as a template directory. Check source .

So maybe there is a hint in this comment. Good luck.

+2
source

Source: https://habr.com/ru/post/1396520/


All Articles