Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 240
fredley Avatar asked Aug 26 '10 12:08

fredley


People also ask

How do I unset an environment variable in Linux?

Using the set -n command Alternatively, you can unset environment variables by using the set command with the “-n” flag.

How do you remove a variable in an environment?

It is very simple to delete environment variables. You need to just use the unset command with the variable name to delete it. This command will remove the variable permanently.

Which command can be used to remove an environment variable?

The dsadmin command can be used for deleting an environment variable in a particular project.


1 Answers

Just

del os.environ['MYVAR'] 

should work.

like image 148
Vinay Sajip Avatar answered Oct 12 '22 21:10

Vinay Sajip