在python中取消设置linux环境变量的正确方法是什么?

时间:2021-04-26 23:09:22

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.

如果平台支持unsetenv()函数,则可以删除此映射中的项目以取消设置环境变量。当从os.environ中删除某个项目时,以及调用其中一个pop()或clear()方法时,将自动调用unsetenv()。

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'] =没有?

2 个解决方案

#1


63  

Just

只是

del os.environ['MYVAR']

should work.

应该管用。

#2


4  

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

您仍然可以从映射中删除项目,但如果unsetenv()不可用,它将不会真正从环境中删除该变量。

del os.environ['MYVAR']

#1


63  

Just

只是

del os.environ['MYVAR']

should work.

应该管用。

#2


4  

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

您仍然可以从映射中删除项目,但如果unsetenv()不可用,它将不会真正从环境中删除该变量。

del os.environ['MYVAR']