python get environment variable example code

Here our topic is, python get environment variable, in simple how to get value of environment variable in python .so we should cover how to set environment variable and using python get environment variable value
To work with environment variables in Python is the same like working with dictionaries. first you need to import the os module.

import os

/* To get the value of all environment variable value in python*/

 

print(os.environ)

 

/* To set the environment variable value by name in python */

# To Set environment variables in python

os.environ['EMAIL'] = '[email protected]' 

os.environ['USER_ID'] = '4568'




/* To get the environment variable value by name in python */

 

# To get environment variables in python

import os
USER_EMAIL = os.environ.get('EMAIL') 
print(USER_EMAIL)
USER_ID = os.environ.get('USER_ID') 
print(USER_ID)

Leave a Reply

Your email address will not be published. Required fields are marked *