How to pass a list as an environment variable?

后端 未结 4 2082
猫巷女王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:55

    The rationale

    I recommend using JSON if you want to have data structured in an environment variable. JSON is simple to write / read, can be written in a single line, parsers exist, developers know it.

    The solution

    To test, execute this in your shell:

    $ export ENV_LIST_EXAMPLE='["Foo", "bar"]'
    

    Python code to execute in the same shell:

    import os
    import json
    
    env_list = json.loads(os.environ['ENV_LIST_EXAMPLE'])
    print(env_list)
    print(type(env_list))
    

    gives

    ['Foo', 'bar']
    
    

    Package

    Chances are high that you are interested in cfg_load

提交回复
热议问题