Fix Python – What is the correct way to unset a linux environment variable in python?

Question

Asked By – fredley

From the documentation:

If the platform supports the unsetenv() function, you can delete items in this mapping to unset environment variables. unsetenv() will be called automatically when an item is deleted from os.environ, and when one of the pop() or clear() methods is called.

However I want something that will work regardless of the availability of unsetenv(). How do I delete items from the mapping if it’s not available? os.environ['MYVAR'] = None?

Now we will see solution for issue: What is the correct way to unset a linux environment variable in python?


Answer

Just

del os.environ['MYVAR']

should work.

This question is answered By – Vinay Sajip

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0