FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Tools/scripts/win_add2path.py at pythoncapi · pythoncapi/cpython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pythoncapi
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Tools
/
scripts
/
win_add2path.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (48 loc) · 1.62 KB
Breadcrumbs
cpython
/
Tools
/
scripts
/
win_add2path.py
Copy path
File metadata and controls
58 lines (48 loc) · 1.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
58
"""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.
"""
import
sys
import
site
import
os
import
winreg
HKCU
=
winreg
.
HKEY_CURRENT_USER
ENV
=
"Environment"
PATH
=
"PATH"
DEFAULT
=
"%PATH%"
def
modify
():
pythonpath
=
os
.
path
.
dirname
(
os
.
path
.
normpath
(
sys
.
executable
))
scripts
=
os
.
path
.
join
(
pythonpath
,
"Scripts"
)
appdata
=
os
.
environ
[
"APPDATA"
]
if
hasattr
(
site
,
"USER_SITE"
):
usersite
=
site
.
USER_SITE
.
replace
(
appdata
,
"%APPDATA%"
)
userpath
=
os
.
path
.
dirname
(
usersite
)
userscripts
=
os
.
path
.
join
(
userpath
,
"Scripts"
)
else
:
userscripts
=
None
with
winreg
.
CreateKey
(
HKCU
,
ENV
)
as
key
:
try
:
envpath
=
winreg
.
QueryValueEx
(
key
,
PATH
)[
0
]
except
OSError
:
envpath
=
DEFAULT
paths
=
[
envpath
]
for
path
in
(
pythonpath
,
scripts
,
userscripts
):
if
path
and
path
not
in
envpath
and
os
.
path
.
isdir
(
path
):
paths
.
append
(
path
)
envpath
=
os
.
pathsep
.
join
(
paths
)
winreg
.
SetValueEx
(
key
,
PATH
,
0
,
winreg
.
REG_EXPAND_SZ
,
envpath
)
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
(
"
\n
PATH is now:
\n
%s
\n
"
%
envpath
)
print
(
"Expanded:"
)
print
(
winreg
.
ExpandEnvironmentStrings
(
envpath
))
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL