Python - temporarily modify the current process's environment

前端 未结 6 1818
清歌不尽
清歌不尽 2020-11-30 05:47

I use the following code to temporarily modify environment variables.

@contextmanager
def _setenv(**mapping):
    \"\"\"``with`` context to temporarily modi         


        
6条回答
  •  野性不改
    2020-11-30 06:00

    _environ = dict(os.environ)  # or os.environ.copy()
    try:
    
        ...
    
    finally:
        os.environ.clear()
        os.environ.update(_environ)
    

提交回复
热议问题