如何在GAE中获得应用程序根路径 [英] How to get application root path in GAE

查看:89
本文介绍了如何在GAE中获得应用程序根路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为GAE Python应用程序使用Jinja2模板。其实在一个项目中有几个小应用程序。例如,他们是博客和网站。所以,第一个是博客,第二个是站点=)。我有这个文件夹结构:

$ $ $ $ $ $ $

$ b $ site $
/ templates
/ blog
/ site

我也有一个代码访问每个应用程序的模板文件夹。它看起来像这样:

  template_dirs = [] 
template_dirs.append(os.path.join(os.path .dirname(__ file__),'templates / project'))

当然,它不起作用因为它是错的。它返回一个像这样的字符串:
base / data / home / apps / myapplication / 1.348460209502075158 / apps / project / templates / project

我需要它返回一个像这样的字符串:
base / data / home / apps / myapplication / 1.348460209502075158 / apps / templates / project
如何使用绝对路径而不是相对路径?我想我需要以某种方式获得我整个GAE项目的根。
Thanks!

解决方案

获取应用程序根路径的最简单方法是将模块放入根目录的应用程序,它存储 os.path.dirname(__ file __)的结果,然后根据需要导入。或者,在位于应用程序根目录的模块上调用 os.path.dirname(module .__ file __)


I am using Jinja2 templates for my GAE Python application. Actually there are a couple of small applications inside one project. They are, for example, blog and site. So, the first one is for blog and the second one is for site =). I have this folders structure:

/
  /apps
    /blog
    /site
/templates
  /blog
  /site

I also have a code for accessing templates folder for each application. It looks like this:

template_dirs = []
template_dirs.append(os.path.join(os.path.dirname(__file__), 'templates/project'))

Of course, it doesn't work as it's wrong. It returns a string like this: base/data/home/apps/myapplication/1.348460209502075158/apps/project/templates/project

And I need it to return a string like this: base/data/home/apps/myapplication/1.348460209502075158/apps/templates/project How can I do that using absolute paths, not relative? I suppose I need to get the root of the my whole GAE project some way. Thanks!

解决方案

The easiest way to get the root path of your app is to put a module in the root of your app, which stores the result of os.path.dirname(__file__), then import that where needed. Alternately, call os.path.dirname(module.__file__) on a module that's in the root of your app.

这篇关于如何在GAE中获得应用程序根路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆