- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathwin_add2path.py
57 lines (47 loc) · 1.58 KB
/
win_add2path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Add Python to the search path on Windows
This is a simple script to add Python to the Windows search path. It
modifies the current user (HKCU) tree of the registry.
Copyright (c) 2008 by Christian Heimes <christian@cheimes.de>
Licensed to PSF under a Contributor Agreement.
"""
importsys
importsite
importos
import_winreg
HKCU=_winreg.HKEY_CURRENT_USER
ENV="Environment"
PATH="PATH"
DEFAULT=u"%PATH%"
defmodify():
pythonpath=os.path.dirname(os.path.normpath(sys.executable))
scripts=os.path.join(pythonpath, "Scripts")
appdata=os.environ["APPDATA"]
ifhasattr(site, "USER_SITE"):
userpath=site.USER_SITE.replace(appdata, "%APPDATA%")
userscripts=os.path.join(userpath, "Scripts")
else:
userscripts=None
with_winreg.CreateKey(HKCU, ENV) askey:
try:
envpath=_winreg.QueryValueEx(key, PATH)[0]
exceptWindowsError:
envpath=DEFAULT
paths= [envpath]
forpathin (pythonpath, scripts, userscripts):
ifpathandpathnotinenvpathandos.path.isdir(path):
paths.append(path)
envpath=os.pathsep.join(paths)
_winreg.SetValueEx(key, PATH, 0, _winreg.REG_EXPAND_SZ, envpath)
returnpaths, envpath
defmain():
paths, envpath=modify()
iflen(paths) >1:
print"Path(s) added:"
print'\n'.join(paths[1:])
else:
print"No path was added"
print"\nPATH is now:\n%s\n"%envpath
print"Expanded:"
print_winreg.ExpandEnvironmentStrings(envpath)
if__name__=='__main__':
main()