views:

30

answers:

2

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?

+3  A: 

Just

del os.environ['MYVAR']

should work.

Vinay Sajip
That works iff `unsetenv` does.
katrielalex
The question was about removing items from the mapping, after all. If unsetenv is unsupported, then the key and value are removed from the mapping but remain set in the environment, neh?
Vinay Sajip
A: 

You can still delete items from the mapping, but it will not really delete the variable from the environment if unsetenv() is not available.

del os.environ['MYVAR']
Sjoerd