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.bat
Go to the documentation of this file.
1@echo off
2setlocal
3REM Windows Command Line Script (Batch) to install the plugin "pyenv-virtualenv".
4REM
5REM Dependencies:
6REM * pyenv
7REM * pyenv-virtualenv
8REM
9REM © 2025 Michael Paul Korthals. All rights reserved.
10REM For legal details see documentation.
11REM
12REM 2025-07-31
13REM
14REM This script is located in the project main directory.
15REM
16REM Simply open it in Windows Explorer or call it by path
17REM in the Windows CMD terminal.
18REM
19REM The script returns RC = 0 or another value in case of
20REM error.
21
22REM Get version string of the package
23set /p VERSION=<"%~dp0.version"
24REM Output installation purpose
25echo.
26echo ␛[92mSUCCESS -----------------------------------------------------------------------␛[0m
27echo ␛[92mSUCCESS Installing/docking plugin "pyenv-virtualenv" v%VERSION% for Windows ...␛[0m
28echo ␛[92mSUCCESS -----------------------------------------------------------------------␛[0m
29echo ␛[37mINFO Setting up plugin folder tree ...␛[0m
30REM Check if "pyenv" variable "PYENV_ROOT" is installed
31if not defined PYENV_ROOT goto error1
32REM Check if "pyenv" root folder exists
33if not exist "%PYENV_ROOT%" goto error2
34REM Ensure that subfolder ".\plugins" exists
35if exist "%PYENV_ROOT%plugins" goto endif1
36 mkdir "%PYENV_ROOT%plugins"
37 set /a RC=%ERRORLEVEL%
38 if %RC% neq 0 goto error3
39:endif1
40REM Ensure that required subfolders exist
41if exist "%PYENV_ROOT%plugins\pyenv-virtualenv" goto endif2
42 REM Make "pyenv-virtualenv" folder tree
43 mkdir "%PYENV_ROOT%plugins\pyenv-virtualenv"
44 set /a RC=%ERRORLEVEL%
45 if %RC% neq 0 goto error4
46:endif2
47REM Copy "pyenv-virtualenv" folder tree
48echo ␛[37mINFO Copying files could take some seconds ...␛[0m
49if defined VERBOSE goto else3
50 xcopy "%~dp0*" "%PYENV_ROOT%plugins\pyenv-virtualenv" /s /e /k /r /v /q /y
51 set /a RC=%ERRORLEVEL%
52 goto endif3
53:else3
54 set /a VERBOSE=%VERBOSE%
55 if %VERBOSE% neq 0 goto else3a
56 xcopy "%~dp0*" "%PYENV_ROOT%plugins\pyenv-virtualenv" /s /e /k /r /v /q /y
57 set /a RC=%ERRORLEVEL%
58 goto endif3a
59 :else3a
60 xcopy "%~dp0*" "%PYENV_ROOT%plugins\pyenv-virtualenv" /s /e /k /r /v /y
61 set /a RC=%ERRORLEVEL%
62 :endif3a
63:endif3
64if %RC% neq 0 goto error5
65REM Patch the file "pyenv.bat"
66REM Check "pyenv" version
67set /P PYENV_VERSION=<"%PYENV_ROOT%..\.version"
68if not exist "%PYENV_ROOT%plugins\pyenv-virtualenv\patch\pyenv_ptc_%PYENV_VERSION%.bat" goto error6
69echo ␛[37mINFO Activating command forwarding from "pyenv" to "pyenv-virtualenv" ...␛[0m
70if exist "%PYENV_ROOT%plugins\pyenv-virtualenv\shims\pyenv.bat" del /f "%PYENV_ROOT%plugins\pyenv-virtualenv\shims\pyenv.bat"
71echo ␛[37mINFO Creating symbolic link "%PYENV_ROOT%plugins\pyenv-virtualenv\shims\pyenv.bat" → "%PYENV_ROOT%plugins\pyenv-virtualenv\patch\pyenv_ptc_%PYENV_VERSION%.bat" ...␛[0m
72REM Check privileges
73net session >nul 2>&1
74if %ERRORLEVEL% == 0 goto else4
75 REM Running with 'User' privileges.
76 REM Bypassing Windows "'mklink' only as 'Administrator'" limitation.
77 choice /C YN /M "␛[94mDo you permit needed 'Administrator' privileges␛[0m"
78 if not %ERRORLEVEL% == 1 goto error7
79 REM IMPORTATNT: Wait until the process returns
80 powershell -command "Start-Process '%~dp0install_mklink.bat' -Verb runAs -Wait"
81 goto endif4
82:else4
83 REM Running with 'Administrator' privileges.
84 powershell -file "%~dp0install_mklink.ps1"
85:endif4
86set /a RC=%ERRORLEVEL%
87if %RC% neq 0 goto error7
88if not exist "%PYENV_ROOT%plugins\pyenv-virtualenv\shims\pyenv.bat" goto else5
89 echo ␛[92mSUCCESS Symbolic link "%PYENV_ROOT%plugins\pyenv-virtualenv\shims\pyenv.bat" → "%PYENV_ROOT%plugins\pyenv-virtualenv\patch\pyenv_ptc_%PYENV_VERSION%.bat" has been written.␛[0m
90 goto endif5
91:else5
92 set /a RC=1
93 goto error7
94:endif5
95REM Audit the 'Machine' PATH and the 'User' PATH for path conflicts
96powershell -file install_audit.ps1
97set /a RC=%ERRORLEVEL%
98if %RC% neq 0 goto finish
99REM Set the 'shims' directory at the beginning of the 'User' PATH
100REM or 'Machine' if needed or possible.
101powershell -file modify_path.ps1
102set /a RC=%ERRORLEVEL%
103if %RC% neq 0 goto finish
104echo.
105echo ␛[92mSUCCESS Plugin package "pyenv-virtualenv" v%VERSION% for Windows is installed (RC = %RC%).␛[0m
106echo.
107echo ␛[95mNOTICE You are recommended to read the unit 'Usage' in the documentation.␛[0m
108echo ␛[37mINFO Execute:␛[0m
109echo ␛[37mINFO "%PYENV_ROOT%plugins\pyenv-virtualenv\docs\html\index.html"␛[0m
110goto finish
111REM Display error messages
112:error1
113set RC=1
114echo.
115echo ␛[91mERROR Variable "PYENV_ROOT" is not set (RC = %RC%).␛[0m
116echo ␛[37mINFO Check/install/configure "pyenv" for Windows. Then try again.␛[0m
117goto finish
118:error2
119set RC=2
120echo.
121echo ␛[91mERROR Directory "%PYENV_ROOT%" not found (RC = %RC%).␛[0m
122echo ␛[37mINFO Check/install/configure "pyenv" for Windows. Then try again.␛[0m
123goto finish
124:error3
125echo.
126echo ␛[91mERROR Cannot make directory "%PYENV_ROOT%plugins" (RC = %RC%).␛[0m
127echo ␛[37mINFO Analyze/configure your file access/permissions to "%PYENV_ROOT%". Then try again.␛[0m
128goto finish
129:error4
130echo.
131echo ␛[91mERROR Cannot make directory tree in "%PYENV_ROOT%plugins" (RC = %RC%).␛[0m
132echo ␛[37mINFO Analyze/configure your file access/permissions to "%PYENV_ROOT%plugins". Then try again.␛[0m
133goto finish
134:error5
135echo.
136echo ␛[91mERROR Failed to install "pyenv-virtualenv" v%VERSION% for Windows (RC = %RC%).␛[0m
137echo ␛[37mINFO Observe the logging why this has going wrong. Reconfigure/repair. Then try again.␛[0m
138goto finish
139:error6
140echo.
141echo ␛[91mERROR Cannot find patch file for actual installed "pyenv" version "%PYENV_VERSION%" (RC = %RC%).␛[0m
142copy /a /y /v "%PYENV_ROOT%bin\pyenv.bat" "%PYENV_ROOT%plugins\pyenv-virtualenv\patch\pyenv_ori_%PYENV_VERSION%.bat"
143dir "%PYENV_ROOT%plugins\pyenv-virtualenv\patch\*.*"
144echo ␛[37mINFO Download the latest "pyenv-virtualenv" version from PyPi. Then try again.␛[0m
145echo ␛[37mINFO Alternatively develop the matching patch file for version "%PYENV_VERSION%" and patch the file manually.␛[0m
146echo ␛[37mINFO See the related chapter in the "Development Manual" in documentation "%PYENV_ROOT%plugins\pyenv-virtualenv\docs\html\index.html".␛[0m
147goto finish
148:error7
149echo.
150echo ␛[91mERROR Cannot create link to patch file for "pyenv" version "%PYENV_VERSION%" in the "shims" directory of this plugin (RC = %RC%).␛[0m
151echo ␛[37mINFO Analyze/configure your file access/permissions to "%PYENV_ROOT%plugins\pyenv-virtualenv\shims" or decide to call this script as 'Administrator'. Then try again.␛[0m
152goto finish
153REM Exit program with return code
154:finish
155if exist "%~dp0.*.~~~" del /f "%~dp0.*.~~~"
156exit /b %RC%
157
158
159REM --- END OF CODE ----------------------------------------------------
160