3

I am creating a web application using googles app engine with the python 3.7 standard environment. I am passing values from the app.yaml file to my main script, however, I cannot seem to pass a list from the yaml file to the main file.

Here is my app.yaml file:

runtime: python37

handlers:
  # This configures Google App Engine to serve the files in the app's static
  # directory.
- url: /static
  static_dir: static


env_variables:
  USERS: 'myemail@email.com'
  USERS2: ['myemai@email.com', 'youremail@email.com']
  USERS3:
  - 'myemail@email.com'
  - 'youremail@email.com'
  USERS4:
    - 'myemail@email.com'
    - 'youremail@email.com'

Here is my python script:

import os

users = os.environ.get('USERS')
users2 = os.environ.get('USERS2')
users3 = os.environ.get('USERS3')
users4 = os.environ.get('USERS4')

The variable users returns 'myemail@email.com' correctly. Though, users2, users3 and users4 all return [] (an empty list).

Anthon
  • 51,019
  • 25
  • 150
  • 211
  • 1
    Environment variables are plain text so you have to "invent" data structures by yourself – iBug Feb 03 '19 at 06:54

1 Answers1

0

Environment variables are text so just getting them with os.environ is not going to work.

You claim you are getting empty lists, but I suspect you are actually getting an empty string. There is nothing in Python 3.7's os.py that does anything like transformation of a string to (empty) list.

Actually app.yaml doesn't seem to be able to handle the sequences in YAML it gets, it could do something like concatenate the sequence entries with a separating character. In any event USERS3 and USERS4 are exactly the same, just different indentation (the sequence for USERS2 is of course different)

I suggest you do the above yourself, while also leave out the superfluous quotes in your YAML:

runtime: python37

handlers:
  # This configures Google App Engine to serve the files in the app's static
  # directory.
- url: /static
  static_dir: static


env_variables:
  USERS: myemail@email.com
  USERS2: myemai@email.com:youremail@email.com
  USERS3: myemail@email.com:youremail@email.com
  USERS4: myemail@email.com:youremail@email.com

And then in your Python do

import os

def listified_env(k):
    x = os.environ.get(k)
    if ':' in x:
        return x.split(':')
    return x

users = listified_env('USERS')
users2 = listified_env('USERS2')
users3 = listified_env('USERS3')
users4 = listified_env('USERS4')
Anthon
  • 51,019
  • 25
  • 150
  • 211