Python | os.environ object

Python Methods and Functions

os.environ in Python — it is a display object that represents the user's environment variables. It returns a dictionary having the user's environment variable as the key and their values ​​as the value.

os.environ leads itself as a python dictionary, so all normal dictionary operations like get and set can be done. We can also change os.environ but any changes will only be effective for the current process in which it was assigned and will not change the value forever .

Syntax: os.environ

Parameter: It is a non-callable object. Hence, no parameter is required

Return Type: This returns a dictionary representing the user's environmental variables

Code # 1: Using os.environ to access environment variables

# Python program to explain the os.environ object

 
# import of the os module

import os

import pprint

  
# Get a list of users
# environment variables

env_var = os.environ

  
# Print the list of users
# environment variables

print ( "User's Environment variable:" )

pprint.pprint ( dict (env_var), width = 1 )

Exit:

 {' CLUTTER_IM_MODULE': 'xim',' COLORTERM': 'truecolor',' DBUS_SESSION_BUS_ADDRESS': 'unix: path = / run / user / 1000 / bus',' DESKTOP_SESSION': 'ubuntu',' DISPLAY': ': 0' , 'GDMSESSION':' ubuntu', 'GJS_DEBUG_OUTPUT':' stderr', 'GJS_DEBUG_TOPICS':' JS '' ERROR; JS '' LOG', 'GNOME_DESKTOP_SESSION_ID':' this-is-deprecatedM ', 'GNEOME_SELLION_ ubuntu', 'GTK_IM_MODULE':' ibus', 'HOME':' / home / ihritik', 'IM_CONFIG_PHASE':' 2', 'JAVA_HOME':' / opt / jdk-10.0.1', 'JOURNAL_STREAM':' 9: 28586', 'JRE_HOME':' / opt / jdk-10.0.1 / jre', 'LANG':' en_IN', 'LANGUAGE':' en_IN: en', 'LESSCLOSE':' / usr / bin / lesspipe ''% s ''% s', 'LESSOPEN':' | '' / usr / bin / lesspipe ''% s', 'LOGNAME':' ihritik', 'PATH':' / usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin : / sbin: / bin: / usr / games: /usr/local/games:/snap/bin:/usr/local/java/jdk-10.0.1/bin: /usr/local/java/jdk-10.0. 1 / jre / bin: /opt/jdk-10.0.1/bin: / opt / jdk-10.0.1 / jre / bin', 'PWD':' / home / ihritik', 'QT4_IM_MODULE':' xim', 'QT_IM_MODULE':' ibus', 'SESSION_MANAGER':' local / hritik: @ / tmp / .ICE-unix / 1127, unix / hritik: / tmp / .ICE-unix / 1127', 'SHELL':' / bin / bash', 'SHLVL':' 2', 'SSH_AUTH_SOCK':' / run / user / 1000 / keyring / ssh', 'TERM':' xterm-256color', 'TEXTDOMAIN':' im-config', ' TEXTDOMAINDIR': '/ usr / share / locale /', 'USER':' ihritik', 'USERNAME':' ihritik', 'VTE_VERSION':' 4804', 'WAYLAND_DISPLAY':' wayland-0', 'XDG_CONFIG_DIRS' : '/ etc / xdg / xdg-ubuntu: / etc / xdg',' XDG_CURRENT_DESKTOP': 'ubuntu: GNOME',' XDG_MENU_PREFIX': 'gnome-', 'XDG_RUNTIME_DIR':' / run / user / 1000', ' XDG_SEAT': 'seat0',' XDG_SESSION_DESKTOP': 'ubuntu',' XDG_SESSION_ID': '2',' XDG_SESSION_TYPE': 'wayland',' XDG_VTNR': '2',' XMODIFIERS': '@ im = ibus',' _': '/ usr / bin / python3'} 

Code # 2: Accessing a specific environment variable

# Python program to explain the os.environ object

 
# import of the os module

import os

 
# Get value
# & # 39; HOME & # 39; environment variable

home = os.environ [ 'HOME' ]

  
# Print value
# & # 39; HOME & # 39; environment variable

print ( "HOME: " , home)

  
# Get the value
# & # 39; JAVA_HOME & # 39; environment variable
# using a get operation from a dictionary

java_home = os.environ.get ( 'JAVA_HOME' )

  
# Print value
# & # 39; JAVA_HOME & # 39; environment variable

print ( "JAVA_HOME: " , java_home)

Output:

 HOME: / home / ihritik JAVA_HOME: /opt/jdk-10.0.1 

Code # 3: Change environment variable

# Python program to explain the os.environ object

  
# import of the os module

import os

 

 
# Print value
# & # 39; JAVA_HOME & # 39; environment variable

print ( "JAVA_HOME: " , os.environ [ ' JAVA_HOME' )

 
# Change value
# & # 39; JAVA_HOME & # 39; environment variable

os.environ [ 'JAVA_HOME' ] = ' / home / ihritik / jdk-10.0.1'

 
# Print changed value
# & # 39 ; JAVA_HOME & # 39; environment variable

print ( "Modified JAVA_HOME : " , os.environ [ ' JAVA_HOME' ])

Exit:

 JAVA_HOME: / opt / jdk-10.0.1 Modified JAVA_HOME: /home/ihritik/jdk-10.0.1 

Code # 4: Add a new environment variable

# Python program to explain the os.environ object

 
# import of the os module

import os

 
# Add new environment variable

os.environ [< / code> 'GeeksForGeeks' ] = ' www.python.engineering'

 
# Get value
# Added environment variable

print ( "GeeksForGeeks:" , os.environ [ 'GeeksForGeeks' ])

Exit:

 GeeksForGeeks: www.python.engineering 

Code # 5: Accessing an environment variable that doesn't exist

# Python program to explain the os.environ object

 
  # import of the os module

import os

 
# Print value
# & # 39; MY_HOME & # 39; environment variable

print ( "MY_HOME: " , os.environ [ ' MY_HOME' ]

 

 
# If the key does not exist
# will throw an error

Exit :

 Traceback (most recent call last): File "osenviron.py", line 8, in print ("MY_HOME:", os .environ ['MY_HOME']) File" /usr/lib/python3.6/os.py ", line 669, in __getitem__ raise KeyError (key) from None KeyError:' MY_HOME' 

Code # 6: Handling an error when accessing an environment variable that does not exist

# Python program for os.environ object explanation

 

 
# import of the os module

import os

 
# Method 1
# Print value
# & # 39; MY_HOME & # 39; environment variable

print ( "MY_HOME: " , os.environ.get ( ' MY_HOME' , "Environment variable does not exist" ))

  

 
# Method 2

try :

print ( "MY_HOME:" , os. environ [ 'MY_HOME' ])

except KeyError: 

print ( "Environment variable does not exist" )

Exit :

 MY_HOME: Environment variable does not exist Environment variable does not exist