How to access environment variable values?

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

    Actually it can be done this away:

    import os
    
    for item, value in os.environ.items():
        print('{}: {}'.format(item, value))
    

    Or simply:

    for i, j in os.environ.items():
        print(i, j)
    

    For view the value in the parameter:

    print(os.environ['HOME'])
    

    Or:

    print(os.environ.get('HOME'))
    

    To set the value:

    os.environ['HOME'] = '/new/value'
    

提交回复
热议问题