def extend(pypath):
    '''
    extend(pypath) adds pypath to the PATH env. variable as defined in the 
    registry, and then notifies applications (e.g. the desktop) of this change.
    Already opened DOS-Command prompt are not updated. Newly opened will have the
    new path (inherited from the updated windows explorer desktop)
    '''
    hKey = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE, 
               r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 
               0, _winreg.KEY_READ | _winreg.KEY_SET_VALUE)
    
    value, typ = _winreg.QueryValueEx (hKey, "PATH")
    vals = value.split(';')
    assert isinstance(vals, list)
    if len(sys.argv) > 1 and sys.argv[1] == 'remove':
        try:
            vals.remove(pypath)
        except ValueError:
            print 'path element', pypath, 'not found'
            return
        print 'removing from PATH:', pypath
    else:
        if pypath in vals:
            print 'path element', pypath, 'already in PATH'
            return
        vals.append(pypath)
        print 'adding to PATH:', pypath
    _winreg.SetValueEx(hKey, "PATH", 0, typ, ';'.join(vals) )
    _winreg.FlushKey(hKey)
    # notify other programs
    SendMessage = ctypes.windll.user32.SendMessageW
    HWND_BROADCAST = 0xFFFF
    WM_SETTINGCHANGE = 0x1A
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, u'Environment')

    extend(pypath) adds pypath to the PATH env. variable as defined in the 
    registry, and then notifies applications (e.g. the desktop) of this change.
    Already opened DOS-Command prompt are not updated. Newly opened will have the
    new path (inherited from the updated windows explorer desktop)
    
recipes/Python/577233_Adding_directory_pythexecutable_system_PATH/recipe-577233.py