Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: extending PATH on Windows? Date: Thu, 18 Feb 2016 06:54:16 -0600 Lines: 145 Message-ID: References: <1d5acb1l234hka82flgiqiq13cbp5ra03c@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de c0cgrfIJ+Q7nkJAaMGSfzQZoaXeRFZhWmAegj0J7i5hg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'warnings': 0.03; 'modified': 0.05; 'paths': 0.05; 'sys': 0.05; '__name__': 0.07; 'executable': 0.07; 'main()': 0.07; 'none):': 0.07; 'val': 0.07; 'scripts': 0.09; 'subject:Windows': 0.09; 'path)': 0.09; 'path.': 0.09; 'portions': 0.09; 'scripts,': 0.09; 'skip:[ 30': 0.09; 'way:': 0.09; 'windowserror': 0.09; 'python': 0.10; 'itself.': 0.11; 'def': 0.13; 'wed,': 0.15; "'__main__':": 0.16; '2016': 0.16; 'concatenated': 0.16; 'err': 0.16; 'key:': 0.16; 'main():': 0.16; 'path:': 0.16; 'pythonwin': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'url:py': 0.16; 'variable.': 0.16; 'well- known': 0.16; 'wrote:': 0.16; 'script.': 0.18; "shouldn't": 0.18; 'skip:` 10': 0.18; 'string,': 0.18; 'try:': 0.18; 'variable': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'ascii': 0.22; 'assumes': 0.22; 'ctypes': 0.22; 'occurs': 0.22; 'sep': 0.22; 'bit': 0.23; 'feb': 0.23; 'dec': 0.23; 'errors': 0.23; 'replacing': 0.23; 'import': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; "doesn't": 0.26; 'skip:_ 20': 0.26; 'error': 0.27; '+0000': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:u 20': 0.28; 'skip:( 20': 0.28; '"no': 0.29; 'forces': 0.29; 'path,': 0.29; 'release.': 0.29; 'environment': 0.29; 'raise': 0.29; 'print': 0.30; 'skip:[ 10': 0.31; "can't": 0.32; 'skip:_ 10': 0.32; 'returned': 0.32; 'url:python': 0.33; 'directory,': 0.33; "skip:' 20": 0.34; 'except': 0.34; 'handle': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'path': 0.35; 'unicode': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'skip:_ 40': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'skip:z 10': 0.38; 'times.': 0.38; 'version': 0.38; 'received:209': 0.38; 'log': 0.38; 'skip:o 20': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'called': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'default': 0.61; 'further': 0.62; 'confirm': 0.62; 'back': 0.62; 'more': 0.63; 'here:': 0.63; 'broadcast': 0.66; 'url:11': 0.72; '(utc),': 0.84; 'cc:addr:ix.netcom.com': 0.84; 'folders.': 0.84; 'url:cpython': 0.84; 'url:scripts': 0.84; 'url:v2': 0.84; '2013,': 0.91; 'dennis': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=VLeuJCghMFk+FjiMLaGw+dnqjWooxE/t7RATcic0dCc=; b=LfCbTI9pIWLO6A3pf0nDPxib+8W3CkD1Wm2OPSSosff7x/nOqAowrLb2YOhAXX5r95 rIvf/I6w3KU1nTc3yUc2pWnnr7QXBJW59cpy0u1scDxYPTxCdrG6mKupUw47k3UWBxI3 CyHt8ejF0kc0sTeQQh4YAfIuRXPyOKgd5A3x4f7ZwJQ9xdjsjZ1DcWt4DLC/hPzXp/gZ 9iw0jIQ20wcmJQykfYfhXydH7rDkOBlhsaDZLPQM/g2bAH+sRnDmWECPbmY6sEGUYOS6 f8U5Q0ApFA5pMlj2HXaEppa0+aTDzKYu2R+houX+xyC352qAifozrorr8RNCOj+1ouAS 8C4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=VLeuJCghMFk+FjiMLaGw+dnqjWooxE/t7RATcic0dCc=; b=dyQCkbvkCWnubVQ0mY6ZCyDGGE2xRlWefwpKtfzAndcEkohKqDQyeiv8qonuj6ao7x 3+Ugh3WPevwNjWTmBPFwCfEL9ygYkUdEqmCm6oMjHQ0j6K1XEElBrGowpqzkaXH6FD7e JHiKaThqePV5yOK8KxJocpDdPQ7o1cPFU7taGXyvYSoiADnMOD/0QWgqhASs6NwXb3vE uREYBBfwAjgwVlphHJi0weXuKMMbrDvT2r6zVc2F6mDw39KwE5J8StYLCpD4+3oXAMK/ ZnuC9kuCEYB22wZ7E+ltxQgGJM4EN+ZPXMj7xNBXPo1thu0b6b+SfxEJlWoRXmzVjNdj drog== X-Gm-Message-State: AG10YOS4PnWvTPUyxUS1OlGr+hxpDQO7GQDg/81pSJ2T8nDqBRCvgdmLx54Yniv0S8pPl82NctRXwTrwwwR2KQ== X-Received: by 10.50.2.70 with SMTP id 6mr3071099igs.74.1455800096696; Thu, 18 Feb 2016 04:54:56 -0800 (PST) In-Reply-To: <1d5acb1l234hka82flgiqiq13cbp5ra03c@4ax.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103112 On Wed, Feb 17, 2016 at 6:53 PM, Dennis Lee Bieber wrote: > On Wed, 17 Feb 2016 17:49:11 +0000 (UTC), Ulli Horlacher > declaimed the following: > >>Thorsten Kampe wrote: >> >>> By the way: there is a script called `win_add2path.py` in your Python >>> distribution >> >>I have >>"Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC >>v.1500 32 bit (Intel)] on win32" >>and there is no "win_add2path.py" >> > C:\Python_x64\Python27\Tools\scripts\win_add2path.py > > PythonWin 2.7.5 (default, Sep 16 2013, 23:11:01) [MSC v.1500 64 bit > (AMD64)] on win32. > Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for > further copyright information. > > But I can't confirm that this is not something added in the ActiveState > release. It's here: https://hg.python.org/cpython/file/v2.7.11/Tools/scripts/win_add2path.py But there are a few issues with this script. * A default value of u"%PATH%" is wrong. That causes the system PATH to be concatenated with itself. * For the user scripts directory, replacing appdata with "%APPDATA%" causes os.path.isdir to always fail. * For QueryValueEx, the only error it should handle is ERROR_FILE_NOT_FOUND. Other OS errors are unlikely, but they shouldn't be masked. * In Python 2, the value returned by QueryValueEx is a unicode string, which this script assumes is ASCII only. So it could die on a UnicodeDecodeError. * REG_EXPAND_SZ should only be used when '%' occurs 2 or more times. Otherwise use REG_SZ. This is important for two-pass environment variable expansion. Thus an existing REG_SZ type should be preserved, if that's reasonable, because the user may be reusing PATH in a REG_EXPAND_SZ variable. * It doesn't broadcast a WM_SETTINGCHANGE message, so it needlessly forces the user to log off and back on in order to use the modified PATH. Here's a new version for Python 2. I generalized the shell-variable replacement to a list of well-known folders. import os import sys import site import ctypes import _winreg import warnings user32 = ctypes.WinDLL('user32', use_last_error=True) HWND_BROADCAST = 0xFFFF WM_SETTINGCHANGE = 0x001A SMTO_ABORTIFHUNG = 0x0002 ERROR_FILE_NOT_FOUND = 0x0002 ERROR_TIMEOUT = 0x05B4 HKCU = _winreg.HKEY_CURRENT_USER ENVIRONMENT = u"Environment" PATH = u"PATH" SCRIPTS = u"Scripts" dir_vars = [u"%APPDATA%", u"%LOCALAPPDATA%", u"%USERPROFILE%", u"%HOMEDRIVE%%HOMEPATH%", u"%ProgramFiles%", u"%ProgramFiles(x86)%"] dir_vals = [_winreg.ExpandEnvironmentStrings(v) for v in dir_vars] def broadcast_change(lparam): if not user32.SendMessageTimeoutW( HWND_BROADCAST, WM_SETTINGCHANGE, 0, ctypes.c_wchar_p(lparam), SMTO_ABORTIFHUNG, 1000, None): err = ctypes.get_last_error() if err != ERROR_TIMEOUT: raise ctypes.WinError(err) def modify(): executable = sys.executable.decode("mbcs") pythonpath = os.path.dirname(os.path.normpath(executable)) scripts = os.path.join(pythonpath, SCRIPTS) if hasattr(site, "USER_SITE"): userpath = site.USER_SITE.decode("mbcs") userscripts = os.path.join(userpath, SCRIPTS) else: userscripts = None with _winreg.CreateKey(HKCU, ENVIRONMENT) as key: try: envpath, dtype = _winreg.QueryValueEx(key, PATH) except WindowsError as e: if e.winerror != ERROR_FILE_NOT_FOUND: raise envpath, dtype = u"", _winreg.REG_EXPAND_SZ if dtype not in (_winreg.REG_SZ, _winreg.REG_EXPAND_SZ): raise TypeError(r"Existing PATH value is not a string.") if dtype == _winreg.REG_SZ and envpath.count(u"%") > 1: warnings.warn('Changing type to REG_EXPAND_SZ.') dtype = _winreg.REG_EXPAND_SZ paths = [envpath] for path in (pythonpath, scripts, userscripts): if path and path not in envpath and os.path.isdir(path): if dtype == _winreg.REG_EXPAND_SZ: for var, val in zip(dir_vars, dir_vals): if val in path: path = path.replace(val, var) break if path in envpath: continue paths.append(path) envpath = os.pathsep.join(paths) _winreg.SetValueEx(key, PATH, 0, dtype, envpath) broadcast_change(ENVIRONMENT) return paths, envpath def main(): paths, envpath = modify() if len(paths) > 1: print "Path(s) added:" print '\n'.join(paths[1:]) else: print "No path was added" print "\nCurrent user PATH is now:\n%s\n" % envpath print "Expanded:" print _winreg.ExpandEnvironmentStrings(envpath) if __name__ == '__main__': main()