How to set environment variables in Python?

前端 未结 11 2032
情话喂你
情话喂你 2020-11-22 04:00

I need to set some environment variables in the Python script and I want all the other scripts that are called from Python to see the environment variables\' set.

If

11条回答
  •  佛祖请我去吃肉
    2020-11-22 04:42

    to Set Variable:

    item Assignment method using key:

    import os    
    os.environ['DEBUSSY'] = '1'  #Environ Variable must be string not Int
    

    to get or to check whether its existed or not,

    since os.environ is an instance you can try object way.

    Method 1:

    os.environ.get('DEBUSSY') # this is error free method if not will return None by default
    

    will get '1' as return value

    Method 2:

    os.environ['DEBUSSY'] # will throw an key error if not found!
    

    Method 3:

    'DEBUSSY' in os.environ  # will return Boolean True/False
    

    Method 4:

    os.environ.has_key('DEBUSSY') #last 2 methods are Boolean Return so can use for conditional statements
    

提交回复
热议问题