在python中取消设置linux环境变量的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3575165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to unset a linux environment variable in python?
提问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 thepop()orclear()methods is called.
如果平台支持该
unsetenv()功能,您可以删除此映射中的项目以取消设置环境变量。unsetenv()当从 os.environ 中删除项目时,以及调用pop()或clear()方法之一时,将自动调用。
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?
但是,无论unsetenv(). 如果项目不可用,如何从映射中删除项目?os.environ['MYVAR'] = None?
采纳答案by Vinay Sajip
Just
只是
del os.environ['MYVAR']
should work.
应该管用。
回答by Sjoerd
You can still delete items from the mapping, but it will not really delete the variable from the environment if unsetenv()is not available.
您仍然可以从mapping 中删除项目,但如果unsetenv()不可用,它不会真正从环境中删除变量。
del os.environ['MYVAR']

