• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Unset Environment Variables in Python

Unset Environment Variables in Python

July 11, 2022 Leave a Comment

To unset environment variables in Python, the easiest way is to remove the variable from the os module environ dictionary with pop().

import os 

os.environ.pop("VARIABLE_TO_UNSET", None)

You can also use del if you know the variable is in the environ dictionary. If the variable is not in the dictionary, you will get a KeyError.

import os

if "VARIABLE_TO_UNSET" in os.environ:
    del os.environ["VARIABLE_TO_UNSET"]

When working with operating systems, the ability to set and unset environment variables easily can be valuable.

You can easily unset environment variables in Python.

With the os module environ dictionary, you can access the environment variables of the operating system.

To unset environment variables in Python, the easiest way is to remove the variable from the os module environ dictionary with pop().

You just need to pass the name of the environment variable to pop().

Below is a simple example showing you how to unset an environment variable in Python.

import os 

os.environ.pop("VARIABLE_TO_UNSET", None)

Using del to Unset Environment Variable in Python

You can also use del if you know the variable is in the environ dictionary. del deletes the item from the dictionary of environment variables.

You do have to be careful here because if the variable is not in the dictionary, you will get a KeyError.

Below is an example of how you can unset an environment variable in Python with del.

import os

if "VARIABLE_TO_UNSET" in os.environ:
    del os.environ["VARIABLE_TO_UNSET"]

Hopefully this article has been useful for you to learn how to unset environment variables in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Remove Commas from String
  • 2.  Deque Peek and Queue Peek Functions in Python
  • 3.  Intersection of Two Lists in Python
  • 4.  Convert Integer to Bytes in Python
  • 5.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 6.  Using Python to Get and Print First N Items in List
  • 7.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 8.  Python Square Root – Finding Square Roots Using math.sqrt() Function
  • 9.  Check if a Number is Divisible by 2 in Python
  • 10.  Python Get Number of Cores Using os cpu_count() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy