The “Surroundings Variables” are a key-value pairs set which might be utilized to maintain the data of the present surroundings. So as to add, get, take away, or modify the surroundings variable, the “os.environ” mapping object of the “os” module is utilized in Python.
On this Python tutorial, we’ll current an in depth information on Python “os.environ” utilizing quite a few examples and by way of the next content material:
What’s the “os.environ” in Python?
In Python, the “os.environ” is utilized to symbolize all of the surroundings variables of the current/present course of as a dictionary. In Python, the “os.environ” is a mapping object that behaves/acts like a dictionary. It may be used to get and set surroundings variables and retailer issues just like the person’s dwelling listing, the trail to the executable file, the present working listing, and others.
Let’s perceive this by way of the beneath examples:
Instance 1: Accessing Surroundings Variables Utilizing “os.environ”
The beneath instance code is used to entry all surroundings variables utilizing the “os.environ” object:
import os, pprint
print(“Person’s Surroundings variable:”)
pprint.pprint(dict(os.environ), width = 1)
Within the above code, the “os.environ” mapping object creates/constructs a dictionary-like object, together with all of the surroundings variables which might be presently outlined. The “pprint.pprint()” is used to fairly print all of the person surroundings variables values.
Output
Instance 2: Accessing Specified/Specific Surroundings Variable Utilizing “os.environ”
The beneath code is used to entry the actual surroundings variables key worth:
import os
print(“Surroundings Variable Worth:”, os.environ[‘HOMEPATH’])
Right here, the “os.environ[]” takes the required surroundings variable key title and retrieves the worth.
Output
Instance 3: Including New Surroundings Variable Utilizing “os.environ”
So as to add or set a brand new surroundings variable, take the beneath instance code:
import os
os.environ[‘Linuxhint’] = ‘Python Information’
print(“Linuxhint:”, os.environ[‘Linuxhint’])
On this code, the “os.environ” object provides an surroundings variable key referred to as “Linuxhint” to the worth “Python Information”. After setting the particular surroundings variable, we entry or name it utilizing its key title.
Output
Instance 4: Modifying Surroundings Variables Utilizing “os.environ”
To switch the surroundings variable, the next instance is utilized in Python:
import os
os.environ[‘Linuxhint’] = ‘Python Information’
print(“Authentic Surroundings Variable Worth:”, os.environ[‘Linuxhint’])
os.environ[‘Linuxhint’] = ‘Welcome to Linux Information’
print(“nModified Surroundings Variable Worth:”, os.environ[‘Linuxhint’])
Within the above code, the “os.environ” object is used so as to add/set the required surroundings variable by specifying the key-value pair. After including, we modified the surroundings variable by reassigning a brand new worth to the important thing:
Output
Instance 5: Accessing and Dealing with Error of Surroundings Variable Which Does Not Exist Utilizing “os.environ”
The “os.environ” object can retrieve a “Key-Error” if the actual surroundings variable doesn’t exist. Right here is an instance code:
import os
attempt:
print(“Linuxhint:”, os.environ[‘Linuxhint’])
besides:
print(“Surroundings Variable Does Not Exist”)
Within the above code, the attempt block executes and retrieves the surroundings/customers variable worth utilizing the “os.environ” object if the surroundings/person variable exists. However If the surroundings variable doesn’t exist, the besides block will execute and show the exception message.
Output
That’s all in regards to the Python os.environ object.
Conclusion
The “os.environ” is a mapping object or built-in dictionary in Python that represents all of the person’s surroundings variable values in key-value pairs. It may be used to entry all or specified surroundings variables, set new surroundings variables, and modify surroundings variables in Python. This Python tutorial has delivered a complete information on Python “os.environ” objects utilizing quite a few examples.