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
install_audit.ps1
Go to the documentation of this file.
1# Windows PowerShell Script to audit the 'Machine' PATH
2# and the 'User' PATH environment variable for path conflicts,
3# which could jeopardize 'pyenv'-related Python calls.
4#
5# Dependencies:
6# (None)
7#
8# © 2025 Michael Paul Korthals. All rights reserved.
9# For legal details see documentation.
10#
11# 2025-07-31
12#
13# This script is located in the 'pyenv-virtualenv' plugin root directory.
14#
15# It will be called by 'install.bat' its parent folder.
16#
17# The script returns RC = 0 or another value in case of error.
18
19$rc = 0
20# 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
24if ($as_admin) {
25 Write-Host "$([char]27)[92mSUCCESS This script has been started with 'Administrator' privileges.$([char]27)[0m"
26} else {
27 Write-Host "$([char]27)[92mSUCCESS This script has been started with 'User' privileges.$([char]27)[0m"
28}
29
30# Get/Check 'PYENV_ROOT' environment variable
31try {
32 $pyenv_root = [System.Environment]::GetEnvironmentVariable('PYENV_ROOT').Trim()
33} catch {
34 $rc = 1
35 Write-Host "$([char]27)[91mERROR Cannot find 'PYENV_ROOT' environment variable. 'pyenv' is missing (RC = $rc).$([char]27)[0m"
36 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
37 exit $rc
38}
39Write-Host "$([char]27)[92mSUCCESS 'pyenv' is installed at: '$pyenv_root'$([char]27)[0m"
40
41# Scan for 'pyenv' in 'Machine' PATH
42$machine_paths = [System.Environment]::GetEnvironmentVariable('PATH', "Machine") -Split ";"
43$found_machine_bin = ""
44$found_machine_shims = ""
45foreach ($item in $machine_paths) {
46 $item1 = $item.Trim()
47 if ($item1 -ne "") {
48 if ($item1 -eq ($pyenv_root + "bin")) {
49 $found_machine_bin = $item1
50 }
51 if ($item1 -eq ($pyenv_root + "shims")) {
52 $found_machine_shims = $item1
53 }
54 }
55}
56Write-Host "$([char]27)[37mINFO 'pyenv' 'bin' on 'Machine' PATH: '$found_machine_bin'$([char]27)[0m"
57Write-Host "$([char]27)[37mINFO 'pyenv' 'shims' on 'Machine' PATH: '$found_machine_shims'$([char]27)[0m"
58
59# Scan for 'pyenv' in 'User' PATH
60$user_paths = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";"
61$found_user_bin = ""
62$found_user_shims = ""
63foreach ($item in $user_paths) {
64 $item1 = $item.Trim()
65 if ($item1 -ne "") {
66 if ($item1 -eq ($pyenv_root + "bin")) {
67 $found_user_bin = $item1
68 }
69 if ($item1 -eq ($pyenv_root + "shims")) {
70 $found_user_shims = $item1
71 }
72 }
73}
74Write-Host "$([char]27)[37mINFO 'pyenv' 'bin' on 'User' PATH: '$found_user_bin'$([char]27)[0m"
75Write-Host "$([char]27)[37mINFO 'pyenv' 'shims' on 'User' PATH: '$found_user_shims'$([char]27)[0m"
76
77# Check if 'pyenv' is installed for 'All Users' or for 'This User Only'
78if (($found_machine_bin -ne "") -and ($found_machine_shims -ne "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
79 Write-Host "$([char]27)[92mSUCCESS 'pyenv' PATH items found in 'Machine' PATH for 'All Users'.$([char]27)[0m"
80} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -ne "") -and ($found_user_shims -ne "")) {
81 Write-Host "$([char]27)[92mSUCCESS 'pyenv' PATH items found in 'User' PATH for 'This User Only'.$([char]27)[0m"
82} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
83 # Not any 'pyenv' PATH item found
84 $rc = 1
85 Write-Host "$([char]27)[91mERROR Cannot find 'pyenv' PATH items (RC = $rc).$([char]27)[0m"
86 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
87 exit $rc
88} else {
89 # Otherwise, the 'pyenv' PATH definition is inconsistent
90 $rc = 1
91 Write-Host "$([char]27)[91mERROR Inconsistent 'pyenv' PATH definition detected (RC = $rc).$([char]27)[0m"
92 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
93 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
94 exit $rc
95}
96
97# Try to find 'pyenv' Python executable without calling Python
98Write-Host "$([char]27)[37mINFO Trying to find 'pyenv' Python executable by command 'where.exe python':$([char]27)[0m"
99where.exe python | Tee-Object -Variable output
100$output_lines = $output -Split "\r\n"
101$conflicts = @()
102$found_python = ""
103foreach ($line in $output_lines) {
104 $line1 = $line.Trim()
105 if ($line.StartsWith($pyenv_root)) {
106 # Detect 'pyenv' Python executable
107 $found_python = $line1
108 break
109 } else {
110 # Detect a conflict with another global Python executable,
111 # which have a higher PATH priority then 'Pyenv'.
112 $conflicts += @(, $line1)
113 }
114}
115if ($found_python -eq "") {
116 Write-Host "$([char]27)[91mERROR Unexpectedly cannot find Python executable.$([char]27)[0m"
117 Write-Host "$([char]27)[37mINFO Install/repair/configure 'pyenv'. Then try again.$([char]27)[0m"
118} else {
119 if ($conflicts.count -eq 0) {
120 Write-Host "$([char]27)[92mSUCCESS Python executable found at '$found_python' (RC = $rc).$([char]27)[0m"
121 } else {
122 $rc = 1
123 Write-Host "$([char]27)[91mERROR Found path conflicts (RC = $rc):$([char]27)[0m"
124 foreach ($conflict in $conflicts) {
125 Write-Host "$([char]27)[91mERROR * '$conflict'.$([char]27)[0m"
126 }
127 Write-Host "$([char]27)[95mNOTICE It is your decision, how to manage your PATH. This program cannot do this for you.$([char]27)[0m"
128 Write-Host "$([char]27)[95mNOTICE To get help, completely read the unit 'Path Conflicts' in the documentation.$([char]27)[0m"
129 Write-Host "$([char]27)[37mINFO Resolve the conflicts adopting the PATH on 'Machine' and 'User' according to your needs. Then try again.$([char]27)[0m"
130 exit $rc
131 }
132}
133
134# Return error level
135exit 0
136
137
138# --- END OF CODE ------------------------------------------------------
139