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_mklink.ps1
Go to the documentation of this file.
1# Windows PowerShell Script to write the 'pyenv.bat'
2# symbolic link to the patched version of that file.
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$rc = 0
19# Check if this script is running as 'Administrator'
20$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
21$as_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
22
23# Get/Check 'PYENV_ROOT' environment variable
24try {
25 $pyenv_root = [System.Environment]::GetEnvironmentVariable('PYENV_ROOT').Trim()
26} catch {
27 $rc = 1
28 Write-Host "$([char]27)[91mERROR Cannot find 'PYENV_ROOT' environment variable. 'pyenv' is missing (RC = $rc).$([char]27)[0m"
29 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
30 exit $rc
31}
32
33# Read 'pyenv' version from file
34try {
35 $pyenv_version = [IO.File]::ReadAllText("$pyenv_root..\.version").Trim()
36} catch {
37 $rc = 1
38 Write-Host "$([char]27)[91mERROR Cannot read 'pyenv' version from file (RC = $rc).$([char]27)[0m"
39 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
40 exit $rc
41}
42
43# Make symbolic link to patched 'pyenv.bat'
44if ($as_admin) {
45 $link_path = $pyenv_root + "plugins\pyenv-virtualenv\shims\pyenv.bat"
46 $link_target = $pyenv_root + "plugins\pyenv-virtualenv\patch\pyenv_ptc_$pyenv_version.bat"
47 Write-Host "$([char]27)[37mINFO Writing symbolic link:$([char]27)[0m"
48 Write-Host "$([char]27)[37mINFO * Path: '$link_path'.$([char]27)[0m"
49 Write-Host "$([char]27)[37mINFO * Target: '$link_target'.$([char]27)[0m"
50 Write-Host "$([char]27)[37mINFO This could take some seconds ...$([char]27)[0m"
51 try {
52 New-Item -ItemType SymbolicLink -Path "$link_path" -Target "$link_target" | Out-Null
53 } catch {
54 $rc = 1
55 Write-Host "$([char]27)[91mERROR Unexpectedly cannot write symbolic link (RC = $rc).$([char]27)[0m"
56 Write-Host "$([char]27)[37mINFO Analyze/configure/repair the situation, why a script running as 'Administrator' fails. Then try again.$([char]27)[0m"
57 exit $rc
58 }
59 Write-Host "$([char]27)[37mINFO Done.$([char]27)[0m"
60} else {
61 $rc = 13
62 Write-Host "$([char]27)[91mERROR Insufficient privileges. (RC = $rc).$([char]27)[0m"
63 Write-Host "$([char]27)[37mINFO To create a symbolic link, you must call this script in a console terminal with 'Administrator' privileges. Then try again.$([char]27)[0m"
64 exit $rc
65}
66
67# Return error level
68exit $rc
69
70
71# --- END OF CODE ------------------------------------------------------
72