def change_env(name, val):
    """
    Args:
        name(str): name of the env var
        val(str or None): the value, or set to None to clear the env var.

    Returns:
        a context where the environment variable ``name`` being set to
        ``val``. It will be set back after the context exits.
    """
    oldval = os.environ.get(name, None)

    if val is None:
        try:
            del os.environ[name]
        except KeyError:
            pass
    else:
        os.environ[name] = val

    yield

    if oldval is None:
        try:
            del os.environ[name]
        except KeyError:
            pass
    else:
        os.environ[name] = oldval

    Args:
        name(str): name of the env var
        val(str or None): the value, or set to None to clear the env var.

    Returns:
        a context where the environment variable ``name`` being set to
        ``val``. It will be set back after the context exits.
    
tensorpack/utils/utils.py