How to access environment variable values?

后端 未结 12 1513
孤独总比滥情好
孤独总比滥情好 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:29

    The original question (first part) was "how to check environment variables in Python."

    Here's how to check if $FOO is set:

    try:  
       os.environ["FOO"]
    except KeyError: 
       print "Please set the environment variable FOO"
       sys.exit(1)
    

提交回复
热议问题