Python os.getenv() Method

We are here to discuss the Python os.getenv() Method but before starting it we should have some knowledge of environment variables in Python.

Python Environment Variables

Environment variables are used by software programs in the system. In environment variables, the path and directory of the software applications are stored.  

We can edit delete and add new variables to it.

We can create and get these environment variables in python by using os.getenv() method.

Introduction to Python os.getenv() Method

The os.getenv method() is used in Python for getting the value of an environment variable.

If the value exists so the method will return the value otherwise it will set the default value to none.

Syntax

os.getenv (key, default = None)

Parameters

Key shows the environment variable name.


The default value returns none if the key value of the environment variable does not exist. Default is an optional parameter.

Return Type

The return type method returns a string that shows the environment variable value of the specified key, in case of a missing key, the return type method will return the default parameter value.

Example 1

import os               #importing os

key = 'py'                # value of environment variable

# using os.getenv() method with default value= 'python/env/var'

value = os.getenv(key,default='python/env/var')   

print(f"Value of 'py' env variable: {value}")

Code Explanation

In the above example we imported the OS module first then we declared a key-value ‘py’ (value of environment variable) after that we used the os.getenv() method, in the default parameter we set a value for the key, which will be displayed only when the key is specified.

Output

Example 2

import os

key='GET_KEY'

print(os.getenv('LANG'))

value=(os.getenv(key,default='hi there'))

print(f"this is the value of 'GET_KEY' variable:{value}")

Code Explanation

In the above code we imported the OS module first and then declared a key-value ‘GET_KEY ’after that we printed a non-declared key value by using getenv() method this method will check the existence of the key ‘LANG’ if it is not the declared key value so the getenv() method will return none, in the next step we used the os.getenv()method and set the default parameter value and then we printed the value, let’s check the result:

Output

As you can see in the output the getenv()method returned none for the non-declared key value and it printed the set default value for the declared key value.

Example 3

os.environ['KEY_UPDATED'] = '400'

print(os.getenv('KEY_UPDATED'))

del os.environ['KEY_UPDATED']

print(os.getenv('KEY_UPDTAED'))

Code Explanation

In the above code we use the os.environ() method for creating an environment variable and we named it “KEY_UPDATED” with the value 400’ then we print the value using os.getenv()method in the next step we use the del os.environ() method for deleting the previously created environment variable in the last we again print the value environment variable using os.getenv() method.

Basically, the above code will check the existence of the key after creating an environment variable and then print it and will delete it, and then will print it again, and surely it will give an error because the key will already been deleted.

Output

As you can see the output returned none because the ‘KEY_UPDATED’ environment variable does not exist.

Conclusion

In this tutorial we discussed a short introduction to environment variables then, we discussed the os.getenv() method in python and did some examples using os.getenv() method in different conditions.

Hope you like the tutorial if you have any queries related to this article feel free to ask below thanks for reading.

Suggested Article:

Leave a Comment