def set_environ(**kwargs):
    """Temporarily set environment variables inside the context manager and
    fully restore previous environment afterwards
    """

    original_env = {k: os.environ.get(k) for k in kwargs}
    os.environ.update(kwargs)
    try:
        yield
    finally:
        for k, v in original_env.items():
            if v is None:
                del os.environ[k]
            else:
                os.environ[k] = v
Temporarily set environment variables inside the context manager and
    fully restore previous environment afterwards
    
stackoverflow/venv/lib/python3.6/site-packages/scrapy/utils/misc.py