pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
modify_path.ps1
Go to the documentation of this file.
1# Windows PowerShell Script to write the PATH item for
2# 'pyenv-virtualenv' for Windows.
3#
4# Dependencies:
5# (None)
6#
7# © 2025 Michael Paul Korthals. All rights reserved.
8# For legal details see documentation.
9#
10# 2025-07-31
11#
12# This script is located in the 'pyenv-virtualenv' plugin root directory.
13#
14# It will be called by 'install.bat' its parent folder.
15#
16# The script returns RC = 0 or another value in case of error.
17
18
19$rc = 0
20# Silently check if this script is running as 'Administrator'
21$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
22$as_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
23
24# Silently Get/Check 'PYENV_ROOT' environment variable
25try {
26 $pyenv_root = [System.Environment]::GetEnvironmentVariable('PYENV_ROOT').Trim()
27} catch {
28 $rc = 1
29 Write-Host "$([char]27)[91mERROR Cannot find 'PYENV_ROOT' environment variable. 'pyenv' is missing (RC = $rc).$([char]27)[0m"
30 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
31 exit $rc
32}
33
34# Silently scan for 'pyenv' and 'pyenv-virtualenv' in 'Machine' PATH
35$machine_paths = [System.Environment]::GetEnvironmentVariable('PATH', "Machine") -Split ";"
36$found_machine_bin = ""
37$found_machine_shims = ""
38$found_machine_plugin_shims = ""
39$index_machine_bin = -1
40$index_machine_shims = -1
41$index_machine_plugin_shims = -1
42$i = -1
43foreach ($item in $machine_paths) {
44 $i += 1
45 $item1 = $item.Trim()
46 if ($item1 -ne "") {
47 if ($item1 -eq ($pyenv_root + "bin")) {
48 $found_machine_bin = $item1
49 $index_machine_bin = $i
50 }
51 if ($item1 -eq ($pyenv_root + "shims")) {
52 $found_machine_shims = $item1
53 $index_machine_shims = $i
54 }
55 if ($item1 -eq ($pyenv_root + "plugins\pyenv-virtualenv\shims")) {
56 $found_machine_plugin_shims = $item1
57 $index_machine_plugin_shims = $i
58 }
59 }
60}
61
62# Silently scan for 'pyenv' and 'pyenv-virtualenv' in 'User' PATH
63$user_paths = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";"
64$found_user_bin = ""
65$found_user_shims = ""
66$found_user_plugin_shims = ""
67$index_user_bin = -1
68$index_user_shims = -1
69$index_user_plugin_shims = -1
70$i = -1
71foreach ($item in $user_paths) {
72 $i += 1
73 $item1 = $item.Trim()
74 if ($item1 -ne "") {
75 if ($item1 -eq ($pyenv_root + "bin")) {
76 $found_user_bin = $item1
77 $index_user_bin = $i
78 }
79 if ($item1 -eq ($pyenv_root + "shims")) {
80 $found_user_shims = $item1
81 $index_user_shims = $i
82 }
83 if ($item1 -eq ($pyenv_root + "plugins\pyenv-virtualenv\shims")) {
84 $found_user_plugin_shims = $item1
85 $index_user_plugin_shims = $i
86 }
87 }
88}
89
90# Check if 'pyenv' is installed for 'All Users' or for 'This User Only'
91if (($found_machine_bin -ne "") -and ($found_machine_shims -ne "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
92 # 'pyenv' PATH items found in 'Machine' PATH for 'All Users'
93 if ($found_machine_plugin_shims -eq "") {
94 if ($as_admin) {
95 $new_machine_path = $pyenv_root + "plugins\pyenv-virtualenv\shims"
96 foreach ($item in $machine_paths) {
97 $new_machine_path += ";" + $item
98 }
99 try {
100 Write-Host "$([char]27)[37mINFO Writing registry. This could take some seconds ...$([char]27)[0m"
101 [System.Environment]::SetEnvironmentVariable('PATH', $new_machine_path, "Machine")
102 } catch {
103 $rc = 1
104 Write-Host "$([char]27)[91mERROR Cannot write 'Machine' PATH environment variable. (RC = $rc).$([char]27)[0m"
105 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
106 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
107 exit $rc
108 }
109 # 'pyenv-virtualenv' PATH item has been correctly written
110 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'Machine' PATH item is now in place. (RC = $rc).$([char]27)[0m"
111 Write-Host "$([char]27)[95mNOTICE Close and reopen the console terminal to let this change take effect.$([char]27)[0m"
112 } else {
113 $rc = 13
114 Write-Host "$([char]27)[91mERROR Insufficient privileges. (RC = $rc).$([char]27)[0m"
115 Write-Host "$([char]27)[37mINFO Changing the 'Machine PATH' you should call this script in a console terminal with 'Administrator' privileges. Then try again.$([char]27)[0m"
116 Write-Host "$([char]27)[95mNOTICE Alternatively read the unit 'Path Conflicts'. Resolve/repair manually. Then try again.$([char]27)[0m"
117 exit $rc
118 }
119 } else {
120 if (($index_machine_plugin_shims -lt $index_machine_bin) -and ($index_machine_plugin_shims -lt $index_machine_shims)) {
121 # 'pyenv-virtualenv' PATH item is already set in place
122 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'shims' on 'Machine' PATH is already in place: '$found_machine_plugin_shims' (RC = $rc).$([char]27)[0m"
123 } else {
124 # 'pyenv-virtualenv' item in 'Machine' PATH
125 # has a lower priority than 'pyenv'.
126 $rc = 1
127 Write-Host "$([char]27)[91mERROR 'pyenv-virtualenv' item in 'Machine' PATH has lower priority than 'pyenv' (RC = $rc).$([char]27)[0m"
128 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
129 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
130 exit $rc
131 }
132 }
133} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -ne "") -and ($found_user_shims -ne "")) {
134 # 'pyenv' PATH items found in 'User' PATH for 'This User Only'
135 if ($found_user_plugin_shims -eq "") {
136 $new_user_path = $pyenv_root + "plugins\pyenv-virtualenv\shims"
137 foreach ($item in $user_paths) {
138 $new_user_path += ";" + $item
139 }
140 try {
141 Write-Host "$([char]27)[37mINFO Writing registry. This could take some seconds ...$([char]27)[0m"
142 [System.Environment]::SetEnvironmentVariable('PATH', $new_user_path, "User")
143 } catch {
144 $rc = 1
145 Write-Host "$([char]27)[91mERROR Cannot write 'User' PATH environment variable. (RC = $rc).$([char]27)[0m"
146 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
147 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
148 exit $rc
149 }
150 # 'pyenv-virtualenv' PATH item has been correctly written
151 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'User' PATH item is now in place. (RC = $rc).$([char]27)[0m"
152 Write-Host "$([char]27)[95mNOTICE Close and reopen the console terminal to let this change take effect.$([char]27)[0m"
153 } else {
154 if (($index_user_plugin_shims -lt $index_user_bin) -and ($index_user_plugin_shims -lt $index_user_shims)) {
155 # 'pyenv-virtualenv' PATH item is already set in place
156 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'shims' on 'User' PATH is already in place: '$found_user_plugin_shims' (RC = $rc).$([char]27)[0m"
157 } else {
158 # 'pyenv-virtualenv' item in 'User' PATH
159 # has a lower priority than 'pyenv'.
160 $rc = 1
161 Write-Host "$([char]27)[91mERROR 'pyenv-virtualenv' item in 'User' PATH has lower priority than 'pyenv' (RC = $rc).$([char]27)[0m"
162 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
163 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
164 exit $rc
165 }
166 }
167} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
168 # Not any 'pyenv' PATH item found
169 $rc = 1
170 Write-Host "$([char]27)[91mERROR Cannot find 'pyenv' PATH items (RC = $rc).$([char]27)[0m"
171 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
172 exit $rc
173} else {
174 # Otherwise, the 'pyenv' PATH definition is inconsistent
175 $rc = 1
176 Write-Host "$([char]27)[91mERROR Inconsistent 'pyenv' PATH definition detected (RC = $rc).$([char]27)[0m"
177 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
178 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
179 exit $rc
180}
181
182# Return error level
183exit $rc
184
185
186# --- END OF CODE ------------------------------------------------------
187