5

I am trying to pass some configuration variables to my main.py from app.yaml. I haven't been able to locate the syntax for accessing app.yaml from the code.

For example you want to have the user put their client number in app.yaml and access it from main.py to pass into main.html. While it would be easy to create a variable in main.py to pass it, it seems to be something that would be better put into app.yaml.

Example:

app.yaml

    application: xyz
    version: 1
    runtime: python27
    ...
    clientID: (ID here)

main.py

    myID = appYAML.clientID
    ...
    values = {'xyz': blah.blah, 'myID': myID }

main.html

    ...
    <script>
      ...
      {% ifequal myID %}
        my_client = {{myID}}
      ...
    </script>

3 Answers 3

10

With the 1.6.5 release, App Engine support this[1]:

- In your app.yaml file, you can include an env_variables stanza that will set
  the given environment variables in your application's runtime.

Information on how to use this is available at: https://cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables.

2
5

That's not supported, and you should put your application-specific settings into your own YAML file.

3
  • 1
    Curiously, this can be done in Java: developers.google.com/appengine/docs/java/config/… Commented Apr 23, 2012 at 19:18
  • I don't see any reason to not do it in app.yaml. There can be times one would want to break out a separate configuration file but for 1 or 2 fields it really doesn't make sense. At any rate I haven't even seen a method for accessing a variable in a different YAML file. Do you have one?
    – Mark Finch
    Commented Apr 23, 2012 at 21:26
  • @MarkFinch: you would need to read the different YAML file and use a YAML parser on it, unless you;re somehow using a framework that does this automatically (which seems unlikely). The problem with using app.yaml for this is that app.yaml isn't uploaded to the server as an application file, so you can't manually read and parse it from within your application.
    – Wooble
    Commented Apr 24, 2012 at 10:19
3

You can define variables in app.yaml to make them available to the program's os.environ dictionary:

env_variables:
  variable_name: '<YOUR VALUE>'

When you need to use this variable within the main.py you can call it in this way:

import os
CUSTOM_SETTINGS = os.environ['variable_name']

Documentation: https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables

3
  • This is great for setting the variables, but as a noob how do you retrieve and use them? Commented Aug 28, 2015 at 21:51
  • @SuatAtanPhD: why would you try to convert a string env var like 'myapp.settings' to an int? Or did you mean to just have DJANGO_SETTINGS_MODULE= os.environ['DJANGO_SETTINGS_MODULE']? Commented Oct 31, 2017 at 18:25
  • @AdamChristianson in this old repository with a Python app is an example, here is the direct line where is used github.com/coto/gae-boilerplate/blob/… Try to clone it and play with the project in order to understand how does it work
    – coto
    Commented Dec 1, 2017 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.