Get Environment Variables in Python

Get Environment Variables in Python

This article will discuss how we can get the value of users’ environment variables in Python.

Use os.environ to Get Environment Variables in Python

In Python, the os module includes functionalities enabling us to interact with the operating system. This module is a part of the standard library of Python.

Using this module, you can portably perform the functionalities dependent on the underlying operating system.

The os.environ is an object in Python that contains the environment variables of the users. When used, it gives a dictionary-like object in which all the environment variables are saved in [key, value] pairs.

Since os.environ acts like a Python dictionary, all the common functions we can perform using a dictionary data structure, like getters and setters, are also available for the environment.

We can also change the value of os.environ, but the changes we make only apply to the current process and will not permanently change the variable’s value.

Syntax:

os.environ

Since it is an object, none of the parameters needs to be passed to it. But if we need to get the name of a specific environment variable, then we can specify its name using indexing like this:

os.environ['name_of_variable']

It will return the value in a dictionary object with the variable’s name as its key and its value as a value. Let’s look at the example below in which we will print all the environmental variables created in the operating system.

import os
import pprint

environment = os.environ
print("List of all Environment variable:")

print(dict(environment))

Output:

get environment variables in python - output one

If we need to get a specific environment variable’s value, such as PATH, then we can get this using the following code snippet:

import os

environment_path = os.environ['PATH']
print("Value of PATH Environment variable: ", environment_path)

Output:

get environment variables in python - output two

In this way, we can get any specific environment variable’s value we want. We can also create a new environment variable or modify some existing variable using this object.

It is demonstrated in the following example:

import os
import pprint

home = os.environ['HOME']
print("Value of HOME before: ", home)

os.environ['HOME']= 'usr/local/bin'
print("Value of HOME after: ", os.environ['HOME'])

get environment variables in python - output three

For creating new environment variables, you can follow the code given below:

import os

os.environ['MY_VAR']='My New Environment Variable'
print("Value of MY_VAR: ", os.environ['MY_VAR'])

Output:

get environment variables in python - output four

Thus, you can see that we can interact directly with the operating system environment variables using the environ object from the os module in Python. We can get, set or modify environment variables using this object.

Related Article - Python Environment

  • Access Environment Variables in Python
  • Read Environment Variables From Env File in Python
  • Set Environment Variables in Python
  • dotenv in Python
  • Set and Get Environment Variables in Python