Environment variables as the name suggests describe the environment of the variables in the system. These variables are a source to provide the information related to the computer. There are two types of environment variables: the user environment variable and the system environment variables. The user environment variables are specific to the user account and the system environment variables are not confined to one user. It may extend to different users in the future.

This write-up will demonstrate different methods to access the environment variables in Python.

How to Access Environment Variables in Python?

The environment variables are just like the dictionaries which store the key-value pair. These variables are retrieved at the run time and stored outside the code. These variables can be accessed using the os.environ module that imports the operating system module. The most common variable accessed is the “PATH” which returns the list of directories the operating system uses to execute the commands. 

Method 1: To Fetch All the Environment Variables

The code below depicts the use of os.environ to fetch the values of the environment variables.

#Import the operating systems module
import os
# get all the environment variables
print(os.environ)

Output

The below output prints all the environment variables using Python.

Method 2: Retrieval of a Single Environment Variable

In the code below a single environment variable “OS” is passed and its value is abstracted using os.environ.

#Import the operating systems module
import os
# The environment variable for OS
print(os.environ['OS'])

Output

The output below depicts that the environment value of OS appears to be Windows_NT.

Method 3: Implementing Dotenv Module

The Dotenv module in Python sets the environment variable in such a way that it reads the key-value pair from the .env file present. The first step involves importing it in Python using the command -m pip install python-dotenv on the command line.

When the module is successfully installed the following message appears,

After the module is successfully installed, now its time to run the code depicted below:

from dotenv import load_dotenv
#Import the OS module
import os
# load the environment variables from the .env file
load_dotenv()
# get the value of the var1 environment variable
var1 = os.environ['OS']
print(var1)

In the above code the:

  • The dotenv module imports the load_dotenv.
  • The os module is also imported.
  • The next step involves loading the environment variables using the load_dotenv().
  • The key is ‘OS’ and the value corresponding to the key is “var1”.
  • The print() function prints the environment variables.

Output

 The output below shows that the environment variable ‘OS’ value is printed.

Method 4: Implementing Argparse Module

The Argparse module is used to handle the command line interfaces. It basically parses(breaks) the value in the declared variables and returns the value for it. Below is the implementation of this method. 

#Import the module
import argparse
#The argparse to parse the command line argument
parser = argparse.ArgumentParser()
parser.add_argument('--OS')
args = parser.parse_args()
OS = args.OS
print(OS)

In the above code block:

  • The argparse module is imported.
  • The variable “OS” is passed to parse the values present in it.
  • The parser.parse_args() basically runs the parser on the variable and returns the data to the args.OS.
  • The print function prints the output.

Output

Since no values were present in the variable “OS” therefore,  “None” is returned as the Output.

Method 5: os.getenv()

This method in Python fetches and returns the value of the environment variable key if the key exists. Below is the implementation of the getenv() method in Python

# Get the value of the PATH variable
import os
main = os.getenv('PATH')
print(main)

Output

The output below shows that the value of the variable “PATH” is printed. 

Conclusion

The values of the environment variables in Python can be fetched using certain objects, methods, and modules in Python that are os.environ object that returns the environment variables along with the Argparse and Dotenv module. The os.getenv() method also performs the same task. This article has demonstrated effective ways to access environment variables in Python.