[Distutils] installing scripts into the user's PATH

Carl Karsten carl at personnelware.com
Mon Jan 7 18:32:21 CET 2008

Speaking of PATH, here is a wad of code I wrote to add a path in Win.  If such a 
thing already exists, I couldn't find it.

pretty much the 2 key lines are:
SetValueEx(h, 'path', 0, REG_EXPAND_SZ, NewPath )
rc, dwReturnValue = win32gui.SendMessageTimeout(win32con.HWND_BROADCAST,....

Add it to the reg, use it now (otherwise it doesn't get used till you reboot.)

Carl K

# addtopath.py
Adds the passed parameter to the windows PATH
Checks to make sure the dir exists and is not already in the path.

import sys
import os
from _winreg import *
import win32gui, win32con

if len(sys.argv)==1:
     # add the python dir to the path
     DirToAdd = os.path.dirname( sys.executable )
     DirToAdd = os.path.abspath(sys.argv[1])

if not os.path.exists(DirToAdd):
     print "dir %s does not exist." % DirToAdd

key=r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
subkey = 'Path'

h = OpenKeyEx( hive, key, 0, KEY_READ )
curPath = QueryValueEx(h, subkey)[0]

if DirToAdd in curPath.split(';'):
     print "dir %s already in current path:" % DirToAdd
     print curPath

print "adding dir %s" % DirToAdd
h = OpenKeyEx( hive, key, 0, KEY_SET_VALUE )
NewPath = curPath + ';' +  DirToAdd
SetValueEx(h, 'path', 0, REG_EXPAND_SZ, NewPath )
print "Path set to", NewPath

rc, dwReturnValue = win32gui.SendMessageTimeout(win32con.HWND_BROADCAST,
	win32con.WM_SETTINGCHANGE, 0, "Environment", win32con.SMTO_ABORTIFHUNG,


More information about the Distutils-SIG mailing list