def save_current_sys_modules():
    """
    Save the current sys modules and restore them when processing is over
    """
    # saved_sys_modules = dict(sys.modules)
    save_setuptools = {}
    for name in sorted(sys.modules.keys()):
        if name == 'setuptools' or name.startswith('setuptools.'):
            save_setuptools[name] = sys.modules.pop(name)

    yield

    # sys.modules = saved_sys_modules
    for name in sorted(sys.modules.keys()):
        if name == 'setuptools' or name.startswith('setuptools.'):
            sys.modules.pop(name)
    for k, v in save_setuptools.items():
        sys.modules[k] = v

    Save the current sys modules and restore them when processing is over
    
bin/pip.py