Os.environ - The Environment Variables Mapping In Python

Variable Name:



  • Several aspects of a system can be defined and controlled through a set of variables, which are called together as the environment. Example: $HOME environment variable defines the pathname of the home directory of the user.
  • The set of environment variables is available to Python through the variable os.environ of the os module.
  • Based on the implementation new variables can be added and removed as well using the os.environ.
  • The Python os module provides a binary or byte strings version as well through os.environb based on the value of os.supports_bytes_environ. The os.supports_bytes_environ is a binary value.


import os


# Print the $HOME environment variable

print("The $Home environment variable as defined:")



# Add a new variable to the environment

newVar = 'TestLogPath'

os.environ[newVar] = os.environ['HOME']+'/'+'TestLogs'


print("Value of the newly added variable named %s:"%(newVar))




The $Home environment variable as defined:


Value of the newly added variable named TestLogPath:



Copyright 2023 © pythontic.com