Python

Python os getenv

In Python, the “os” module is utilized to provide several functions to interact with the operating system. This module also provides functions to work with the environment variables, such as adding or setting environment variables, accessing environment variables, and others. The “os.getenv()” method of the “os” module is used to get the environment variable key value.

This article presents a comprehensive tutorial on Python’s “os.getenv()” method using several examples.

What is the “os.getenv()” Method in Python?

In Python, the “os.getenv()” method of the “os” module is utilized to retrieve the environment variable key value if it is present. But if the environment variable key value does not present, then the default “None” value will be returned.

Syntax

os.getenv(key, default)

 

Parameters

In the above syntax:

  • The “key” parameter is mandatory and is used to indicate the name of the variable key environment.
  • The optional “default” parameter sets the “None” value if the key does not exist.

Return Value

The “os.getenv()” method is used to retrieve the string representation of the environment variable key value if the “key” exists. The default “None” value is returned if the key does not exist.

Example 1: Printing Environment Variable Key Value Using “os.getenv()” Method

The below code is utilized to print the environment variable key value:

import os
print (os.getenv("HOMEPATH"))

 

Here in this code, the “os.getenv()” method takes the specified key “Path” as an argument and prints the environment variable value.

Output

The environment variable value of the specified key has been returned in the above output.

Example 2: Printing Environment Variable Key Value if Key Does Not Exist

Let’s utilize the following code to print the environment variable value of the key that does not exist:

import os
print (os.getenv("HOME", default=None))

 

In the above code, the “os.getenv()” method takes the key “HOME” and “default=None” as an argument to retrieve the value of the environment variable key.

Output

The default value None is retrieved in the above output.

Example 3: Printing Environment Variable Key Value by Specifying Default Parameter Value of “os.getenv()” Method

Let’s take a look at the following example:

import os
print (os.getenv("HOME", default='Key Does Not Exist'))

 

In this code, the specified default value is passed to the “default” parameter along with the key parameter to retrieve the environment variable key value.

Output

The environment variable key value has been returned.

Conclusion

The “os.getenv()” method of the “os” module is utilized in Python to retrieve the environment variable key value if it is present. If the environment/user variable does not exist/present, the default “None” is returned. We can also specify the default value. This article delivered a detailed guide on the “os.getenv()” method using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.