Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103112

Re: extending PATH on Windows?

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: extending PATH on Windows?
Date 2016-02-18 06:54 -0600
Message-ID <mailman.248.1455800105.22075.python-list@python.org> (permalink)
References (1 earlier) <mailman.160.1455625190.22075.python-list@python.org> <n9v58k$dcl$1@news2.informatik.uni-stuttgart.de> <mailman.168.1455630261.22075.python-list@python.org> <na2bqn$7d7$1@news2.informatik.uni-stuttgart.de> <1d5acb1l234hka82flgiqiq13cbp5ra03c@4ax.com>

Show all headers | View raw


On Wed, Feb 17, 2016 at 6:53 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Wed, 17 Feb 2016 17:49:11 +0000 (UTC), Ulli Horlacher
> <framstag@rus.uni-stuttgart.de> declaimed the following:
>
>>Thorsten Kampe <thorsten@thorstenkampe.de> 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()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-16 08:30 +0000
  Re: extending PATH on Windows? Thorsten Kampe <thorsten@thorstenkampe.de> - 2016-02-16 13:19 +0100
    Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-16 12:38 +0000
      Re: extending PATH on Windows? Thorsten Kampe <thorsten@thorstenkampe.de> - 2016-02-16 14:38 +0100
      Re: extending PATH on Windows? Thorsten Kampe <thorsten@thorstenkampe.de> - 2016-02-16 14:43 +0100
        Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-17 17:49 +0000
          Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-17 13:02 -0600
            Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-17 20:29 +0000
              Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-17 15:17 -0600
                Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-17 22:37 +0000
                Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-18 16:55 +0000
                Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-19 12:07 -0600
          Re: extending PATH on Windows? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-17 19:53 -0500
            Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-18 16:24 +0000
              Re: extending PATH on Windows? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-18 21:57 -0500
                Re: extending PATH on Windows? pyotr filipivich <phamp@mindspring.com> - 2016-02-18 19:39 -0800
                Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-19 10:42 +0000
                Re: extending PATH on Windows? Chris Angelico <rosuav@gmail.com> - 2016-02-19 21:48 +1100
                Re: extending PATH on Windows? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-19 07:45 -0500
                Re: extending PATH on Windows? Gisle Vanem <gvanem@yahoo.no> - 2016-02-19 15:18 +0100
                Re: extending PATH on Windows? pyotr filipivich <phamp@mindspring.com> - 2016-02-19 09:04 -0800
                Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-19 12:04 -0600
                Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-19 12:42 -0600
                Re: extending PATH on Windows? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-19 19:57 -0500
                Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-20 07:31 -0600
          Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-18 06:54 -0600
            Re: extending PATH on Windows? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-18 16:50 +0000
  Re: extending PATH on Windows? eryk sun <eryksun@gmail.com> - 2016-02-16 07:17 -0600

csiph-web