How to set environment variables in Python?

前端 未结 11 1751
情话喂你
情话喂你 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:36

    What about os.environ["DEBUSSY"] = '1'? Environment variables are always strings.

    0 讨论(0)
  • 2020-11-22 04:38

    os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set operations mentioned in the other answers, we can also simply check if a key exists. The keys and values should be stored as strings.

    Python 3

    For python 3, dictionaries use the in keyword instead of has_key

    >>> import os
    >>> 'HOME' in os.environ  # Check an existing env. variable
    True
    ...
    

    Python 2

    >>> import os
    >>> os.environ.has_key('HOME')  # Check an existing env. variable
    True
    >>> os.environ.has_key('FOO')   # Check for a non existing variable
    False
    >>> os.environ['FOO'] = '1'     # Set a new env. variable (String value)
    >>> os.environ.has_key('FOO')
    True
    >>> os.environ.get('FOO')       # Retrieve the value
    '1'
    

    There is one important thing to note about using os.environ:

    Although child processes inherit the environment from the parent process, I had run into an issue recently and figured out, if you have other scripts updating the environment while your python script is running, calling os.environ again will not reflect the latest values.

    Excerpt from the docs:

    This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly.

    os.environ.data which stores all the environment variables, is a dict object, which contains all the environment values:

    >>> type(os.environ.data)  # changed to _data since v3.2 (refer comment below)
    <type 'dict'>
    
    0 讨论(0)
  • 2020-11-22 04:38

    When you play with environment variables (add/modify/remove variables), a good practice is to restore the previous state at function completion.

    You may need something like the modified_environ context manager describe in this question to restore the environment variables.

    Classic usage:

    with modified_environ(DEBUSSY="1"):
        call_my_function()
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 04:42

    You should assign string value to environment variable.

    os.environ["DEBUSSY"] = "1"

    If you want to read or print the environment variable just use

    print os.environ["DEBUSSY"]

    This changes will be effective only for the current process where it was assigned, it will no change the value permanently. The child processes will automatically inherit the environment of the parent process.

    0 讨论(0)
提交回复
热议问题