import sys
import os
import time
import _winreg
import ctypes
def extend(pypath):
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')
def find_python():
hKey = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
r'Python.File\shell\open\command')
# get the default value
value, typ = _winreg.QueryValueEx (hKey, None)
program = value.split('"')[1]
if not program.lower().endswith(r'\python.exe'):
return None
return os.path.dirname(program)
pypath=find_python()
extend(pypath)