How to access environment variable values?

后端 未结 12 1362
孤独总比滥情好
孤独总比滥情好 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条回答
  •  旧时难觅i
    2020-11-22 00:33

    To check if the key exists (returns True or False)

    'HOME' in os.environ
    

    You can also use get() when printing the key; useful if you want to use a default.

    print(os.environ.get('HOME', '/home/username/'))
    

    where /home/username/ is the default

提交回复
热议问题