Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get application root path in GAE

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!

like image 756
Sergei Basharov Avatar asked Feb 19 '11 11:02

Sergei Basharov


People also ask

How do I find application root path?

To simply access the app's root path, use the module as though it were a string: var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module. js'); Side note: the module actually returns an object, but that object implements the toString method, so you can use it as though it were a string.

What is application root directory?

The application root directory is the physical directory on your disk the IIS uses as the starting-point for your applications on the web. You create the application root directory (usually called "ISAPI") in the GAS installation directory path, for example, $FGLASDIR\ISAPI.

What is a app Yaml file?

The app. yaml file defines your configuration settings for your runtime as well as general app, network, and other resource settings.

How do I remove default service from App Engine?

The only way you can delete the default version of your App Engine app is by deleting your project. However, you can stop the default version in the GCP Console. This action shuts down all instances associated with the version. You can restart these instances later if needed.


3 Answers

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.

like image 113
Nick Johnson Avatar answered Oct 09 '22 15:10

Nick Johnson


Why not put an os.path.abspath around file

template_dirs.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/project'))
like image 45
Tom Willis Avatar answered Oct 09 '22 14:10

Tom Willis


It's kind of a kludge, and I didn't write this with the love and caring I write most of my code with, but maybe this will be useful for you...

import os
def app_root():
    """Get the path to the application's root directory."""
    app_id = os.environ['APPLICATION_ID']
    path = os.environ['PATH_TRANSLATED']
    path_parts = path.split(app_id, 1)
    root_path = path_parts[0] + app_id
    # If this is ran on Google's servers, there is an extra dir
    # that needs to be traversed to get to the root
    if not os.environ['SERVER_SOFTWARE'].startswith('Dev'):
        root_path += '/' + path_parts[1].lstrip('/').split('/', 1)[0]
    return root_path

Please note that for this to work on the SDK your application's root directory MUST be named the same as your application's ID.

Additionally, this makes assumptions about the directory structure used by Google on the production servers, so it's entirely possible that they could change something and break this.

like image 1
Noah McIlraith Avatar answered Oct 09 '22 13:10

Noah McIlraith