Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103004
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | eryk sun <eryksun@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: extending PATH on Windows? |
| Date | Tue, 16 Feb 2016 07:17:24 -0600 |
| Lines | 101 |
| Message-ID | <mailman.165.1455628688.22075.python-list@python.org> (permalink) |
| References | <n9umo3$9lg$1@news2.informatik.uni-stuttgart.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de 9TcQW0U4NGOV79hjjHTb5AOU8OSCo2fbcr2eTC9XVKUw== |
| Return-Path | <eryksun@gmail.com> |
| 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; '16,': 0.03; 'value,': 0.03; 'string.': 0.04; 'elements.': 0.05; 'importerror:': 0.05; 'paths': 0.05; 'sys': 0.05; 'none):': 0.07; 'session.': 0.07; 'url:msdn': 0.07; 'subject:Windows': 0.09; '[1]:': 0.09; 'windowserror': 0.09; 'def': 0.13; '"%"': 0.16; '2016': 0.16; 'err': 0.16; 'mutliple': 0.16; 'notifies': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'registry.': 0.16; 'reload': 0.16; 'wrote:': 0.16; 'copied': 0.18; 'try:': 0.18; 'variable': 0.18; 'windows': 0.20; 'cc:2**0': 0.20; 'ctypes': 0.22; 'problem:': 0.22; "user's": 0.22; 'am,': 0.23; 'feb': 0.23; 'component': 0.23; 'references': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'command': 0.26; 'skip:" 20': 0.26; 'skip:_ 20': 0.26; 'least': 0.27; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'skip:u 20': 0.28; 'extending': 0.29; 'far,': 0.29; 'environment': 0.29; 'raise': 0.29; 'query': 0.30; 'skip:[ 10': 0.31; 'especially': 0.32; '[1]': 0.32; 'skip:_ 30': 0.33; 'builds': 0.33; "skip:' 20": 0.34; 'tue,': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'path': 0.35; 'replace': 0.35; 'something': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'presence': 0.38; 'received:209': 0.38; 'skip:o 20': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'skip:w 40': 0.66; 'started.': 0.66; 'url:en-us': 0.72; '2:30': 0.84 |
| 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=KFUv1YzUp2jqenOYfzan4VCAO8yVnpdHeQo+wMJN+5M=; b=YRn1oDGC9bOaR06KNKAFDVZ81GIu2h5gZo5BGI40ZG9MG5kG1ZJCLFqeUILxzKj4NQ fM6OOPlIpbGzxfmdrk78TQAFlYfBH3g10gg8C9/BnVK7j0Mt5wW0Ch+C/Gl2VIfOr+C/ WwJ6rM7YrwUOj0PrmYR6PbpGGuR5tgEVVn8214k5apWF+tkBK0mmbryTjO8tqFUGv4tU Daow1Z5qf1HCvi38uQ+ifH3XSzT3osSt2JK7kXcTRUy95AziF4/9j7f3n3wYj0NsHiyE 5s28daCaUvJ+F7JDn4PcZeZ/LqdtGIojcTEOIEI6Lw5tD91xq9Z6tgC+dBMR+SNMU6sT +I8w== |
| 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=KFUv1YzUp2jqenOYfzan4VCAO8yVnpdHeQo+wMJN+5M=; b=mVz/82BW5uS5dYKCRvjCRMpteCUPb7dkCxvIKaiVh6RMQdGvcOUkPjwW+uWgT0EpNN Ri7ULmqqib/1ivAYeBm3drmAeAdMyvhWQCnCJ21vUQdlGHFTbxhxw7l8UAEoDvZY0PxM J01ujC8eu22GRJessASQbb+sszDqi3eXlmj9G7yNPD1BNZWGQGWZAS7WzlOUsScBVhBa 1RBxUA84yB5ILVATPD7geJyLo3O+9rrzakRJEy7FZSpncEShpI+P/I4nUl7dTgqiBpNS 6v5vd/JIHz+inR2mf7zB7jnGrFgIEUtwFrNd8QpZ7SwyaMVxWgnbnG6g76wGvzFp3TC8 TVfQ== |
| X-Gm-Message-State | AG10YOQPQnrI16cOwHvK/c7auptc0ERuw7zBMLp7RWE4ShBDEbVMKb7KLhNjM0+s6CVcpr8XztkxEnoBYsuuhg== |
| X-Received | by 10.50.132.6 with SMTP id oq6mr18788763igb.32.1455628684515; Tue, 16 Feb 2016 05:18:04 -0800 (PST) |
| In-Reply-To | <n9umo3$9lg$1@news2.informatik.uni-stuttgart.de> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:103004 |
Show key headers only | View raw
On Tue, Feb 16, 2016 at 2:30 AM, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
>
> So far, I use:
>
> system('setx PATH "%PATH%;'+bindir+'"')
>
> The problem: In a new process (cmd.exe) PATH contains a lot of double
> elements. As far as I have understood, Windows builds the PATH
> environment variable from a system component and a user component. With
> the setx command from above I have copied the system PATH into the user
> PATH component.
setx broadcasts a WM_SETTINGCHANGE [1] message that notifies Explorer
to reload its environment from the registry, so the user doesn't have
to start a new session. It also decides whether to use REG_SZ or
REG_EXPAND_SZ depending on the presence of mutliple "%" characters in
the string.
[1]: https://msdn.microsoft.com/en-us/library/ms725497
But as you note it's no good for extending an existing value,
especially not for PATH or a value that references other "%variables%"
that you want to remain unexpanded. To do this right, you have to at
least use winreg to query the user's PATH value from the registry. But
then you may as well replace setx completely. Here's a little
something to get you started.
import os
import sys
import types
import ctypes
user32 = ctypes.WinDLL('user32', use_last_error=True)
try:
import winreg
except ImportError:
import _winreg as winreg
def extend_path(new_paths, persist=True):
if isinstance(new_paths, getattr(types, 'StringTypes', str)):
new_paths = [new_paths]
new_paths = [os.path.abspath(p) for p in new_paths]
paths = [p for p in os.environ.get('PATH', '').split(os.pathsep) if p]
for p in new_paths:
if p not in paths:
paths.append(p)
os.environ['PATH'] = os.pathsep.join(paths)
if persist:
_persist_path(new_paths)
def _persist_path(new_paths):
if sys.version_info[0] == 2:
temp_paths = []
for p in new_paths:
if isinstance(p, unicode):
temp_paths.append(p)
else:
temp_paths.append(p.decode('mbcs'))
new_paths = temp_paths
with winreg.OpenKey(winreg.HKEY_CURRENT_USER,
'Environment', 0,
winreg.KEY_QUERY_VALUE |
winreg.KEY_SET_VALUE) as hkey:
try:
user_path, dtype = winreg.QueryValueEx(hkey, 'PATH')
except WindowsError as e:
ERROR_FILE_NOT_FOUND = 0x0002
if e.winerror != ERROR_FILE_NOT_FOUND:
raise
paths = []
else:
if dtype in (winreg.REG_SZ, winreg.REG_EXPAND_SZ):
paths = [p for p in user_path.split(os.pathsep) if p]
else:
paths = []
for p in new_paths:
if p not in paths:
paths.append(p)
pathstr = os.pathsep.join(paths)
if pathstr.count('%') < 2:
dtype = winreg.REG_SZ
else:
dtype = winreg.REG_EXPAND_SZ
winreg.SetValueEx(hkey, 'PATH', 0, dtype, pathstr)
_broadcast_change(u'Environment')
def _broadcast_change(lparam):
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
ERROR_TIMEOUT = 0x05B4
wparam = 0
if not user32.SendMessageTimeoutW(
HWND_BROADCAST, WM_SETTINGCHANGE,
wparam, ctypes.c_wchar_p(lparam),
SMTO_ABORTIFHUNG, 1000, None):
err = ctypes.get_last_error()
if err != ERROR_TIMEOUT:
raise ctypes.WinError(err)
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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