How to access environment variable values?

后端 未结 12 1521
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:52

I set an environment variable that I want to access in my Python application. How do I get its value?

12条回答
  •  执笔经年
    2020-11-22 00:45

    If you are planning to use the code in a production web application code,
    using any web framework like Django/Flask, use projects like envparse, using it you can read the value as your defined type.

    from envparse import env
    # will read WHITE_LIST=hello,world,hi to white_list = ["hello", "world", "hi"]
    white_list = env.list("WHITE_LIST", default=[]) 
    # Perfect for reading boolean
    DEBUG = env.bool("DEBUG", default=False)
    

    NOTE: kennethreitz's autoenv is a recommended tool for making project specific environment variables, please note that those who are using autoenv please keep the .env file private (inaccessible to public)

提交回复
热议问题