How to pass a list as an environment variable?

后端 未结 4 2078
猫巷女王i
猫巷女王i 2021-02-07 01:52

I use a list as part of a Python program, and wanted to convert that to an environment variable.

So, it\'s like this:

list1 = [\'a.1\',\'b.2\',\'c.3\']
         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 02:36

    The environs PyPI package handles my use case well: load a single setting from env var and coerce it to a list, int, etc:

    from environs import Env
    
    env = Env()
    env.read_env()  # read .env file, if it exists
    # required variables
    gh_user = env("GITHUB_USER")  # => 'sloria'
    secret = env("SECRET")  # => raises error if not set
    
    # casting
    max_connections = env.int("MAX_CONNECTIONS")  # => 100
    ship_date = env.date("SHIP_DATE")  # => datetime.date(1984, 6, 25)
    ttl = env.timedelta("TTL")  # => datetime.timedelta(0, 42)
    
    # providing a default value
    enable_login = env.bool("ENABLE_LOGIN", False)  # => True
    enable_feature_x = env.bool("ENABLE_FEATURE_X", False)  # => False
    
    # parsing lists
    gh_repos = env.list("GITHUB_REPOS")  # => ['webargs', 'konch', 'ped']
    coords = env.list("COORDINATES", subcast=float)  # => [23.3, 50.0]
    

提交回复
热议问题