How to Get, Set, and Delete Environment Variables with os.environ in Python

To manage environment variables in Python, use the “os.environ” dicitonary object.

Accessing all the environment variables

import os
import pprint

environ_var = os.environ

# Print the environment variables
print("User's Environment variable:")
pprint.pprint(dict(environ_var), width=1)

Output

Accessing and printing all the environment variables

Accessing specific environment variables

To get the value of an environment variable, use os.environ.get(‘VARIABLE_NAME’). This function returns the value of the specified environment variable if it exists; otherwise, None.

import os

environ_shell = os.environ.get("SHELL")
environ_pwd = os.environ.get("PWD")

print(environ_shell)

print(environ_pwd)

Output

/bin/zsh

/Users/krunallathiya/Desktop/Code/pythonenv/env

Setting environment variables

To set an environment variable, directly assign a value to a key in os.environ. This will set the environment variable for the current and child processes.

import os

# SET AN ENVIRONMENT VARIABLE
os.environ['VARIABLE_NAME'] = 'value'

# GET AND PRINT THAT VARIABLE
print(os.environ.get('VARIABLE_NAME'))

Output

value

Deleting environment variables

To delete an environment variable, use the del statement.

import os

# SET AN ENVIRONMENT VARIABLE
os.environ['VARIABLE_NAME'] = 'value'

print("Before deleting")

# GET AND PRINT THAT VARIABLE
print(os.environ.get('VARIABLE_NAME'))

# DELETE AN ENVIRONMENT VARIABLE
try:
  del os.environ['VARIABLE_NAME']
except KeyError:
  print("Variable does not exist")

print("After deleting")
print(os.environ.get('VARIABLE_NAME'))

Output

Before deleting
value

After deleting
None

KeyError: ‘VARIABLE_NAME’

If the environment variable does not exist, it will raise a KeyError.

import os

del os.environ['VARIABLE_NAME']

Output

KeyError: 'VARIABLE_NAME'

To handle this error, you can use the try-except block:

import os

try:
  del os.environ['VARIABLE_NAME']
except KeyError:
  print("Variable does not exist")

Output

Variable does not exist

Changes made using os.environ are only effective within the current process and its child processes. They do not affect the environment variables of the parent process or the system as a whole.

Clearing a single environment variable

The os.environ.pop() method removes a specific environment variable by its key.

If the key exists, it returns the value and removes it from the dictionary.

If the key does not exist and a default value is provided, it returns the default value; otherwise, it raises a KeyError.

import os

# SET AN ENVIRONMENT VARIABLE
os.environ['VARIABLE_NAME'] = 'value'

# Remove a specific environment variable
# Returns None if the variable does not exist
variable_value = os.environ.pop('VARIABLE_NAME', None)

print(variable_value)

Output

value

Clearing all environment variables

The os.environ.clear() method removes all environment variables, clearing the os.environ dictionary.

import os

# Clear all environment variables
os.environ.clear()

print(dict(os.environ))

That’s it!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.