code: def environment_as(**kwargs):
    
    new_environment = kwargs
    old_environment = {}
  
    def setenv(key, val):
      if val is not None:
        os.environ[key] = val if PY3 else _os_encode(val)
      else:
        if key in os.environ:
          del os.environ[key]
  
    for key, val in new_environment.items():
      old_environment[key] = os.environ.get(key)
      setenv(key, val)
    try:
      yield
    finally:
      for key, val in old_environment.items():
        setenv(key, val)
docs: Update the environment to the supplied values, for example:
  
    with environment_as(PYTHONPATH='foo:bar:baz',
                        PYTHON='/usr/bin/python2.7'):
      subprocess.Popen(foo).wait()