HOWTO · Python

How to Unset a Linux Environment Variable in Python

This article demonstrates how to unset a Linux environment variable in Python.

On this page

To remove an environment variable from a Python program, delete its key from os.environ. The change applies to that Python process and programs it starts afterward; it cannot change the environment of the shell that launched it.

Unset an environment variable in Python

Use os.environ.pop(name, None) to remove a variable without requiring it to be present first. The second argument is returned when the key is absent, so this form is convenient when the variable may or may not exist.

This complete example creates a controlled variable, removes it, and checks the mapping afterward:

import os

name = "DEMO_MODE"
os.environ[name] = "preview"
previous_value = os.environ.pop(name, None)
print(previous_value)
print(name in os.environ)

The first printed value is the value that was stored before removal; the membership check is false after the key has been removed. Choose your own variable name when adapting the example. Avoid using a system variable such as PATH for a demonstration, since changing it can affect programs started later by the same Python process.

Changes made through os.environ update the process environment used for subsequently started child processes. A child receives the environment as it exists when that child starts. Removing a variable in Python does not reach backward and alter the parent shell or other already running processes.

Choose the right Python API for removing a variable

os.environ is Python’s mapping for the process environment. Its pop() method both removes the key from the mapping and returns its former value. Use os.environ.pop() with a default such as None when absence is expected and should be handled quietly.

You can also use del os.environ[name] when the key is expected to exist. It removes the key, but raises KeyError if it is missing. To avoid that error, check if name in os.environ before deleting, or use pop(name, None).

os.unsetenv() is a function in the os module, not a method on os.environ. Python documents it as a lower-level operation; direct calls do not update the os.environ mapping. For ordinary Python code, use the mapping operation so the mapping and process environment stay synchronized. Do not call os.environ.unsetenv(): that method does not exist.

Removing a key is different from assigning it an empty string. For example, os.environ["DEMO_MODE"] = "" leaves the key present with an empty value. Use pop() or del when the variable should be absent.

Handle missing variables and empty values

An environment variable has two separate states relevant here: it can be absent, or it can be present with a value. An empty string is still a value, so checking name in os.environ distinguishes a present empty value from a missing key. os.environ.get(name) returns None for a missing key by default, while it returns "" when the key exists with an empty value.

For optional cleanup, os.environ.pop(name, None) handles either state of existence without raising KeyError: it removes a present key and returns its previous value, or returns None if the key was absent. If you need to distinguish those outcomes, test membership before removal or use a sentinel object as the default. Remember that an environment value is text, so an empty string is distinct from the None default used by get() or pop().

Unset an environment variable in the shell

To remove a variable from the current shell, run the shell’s unset command there, for example unset DEMO_MODE in a POSIX shell. This is a shell command, not a Python command. The shell then omits that variable from the environment it passes to programs it starts afterward.

A Python process cannot modify its parent shell because a child process cannot change the environment of its parent. If Python removes a variable from its own os.environ, that affects Python and later children it launches, but the shell remains unchanged. To make a change persist in your interactive shell, run unset DEMO_MODE directly in that shell; export is used to mark shell variables for export to child processes and does not remove them.