2REM Windows Command Line Script (Batch) to activate virtual environment.
6REM * pyenv-virtualenv-init
8REM © 2025 Michael Paul Korthals. All rights reserved.
9REM For legal details see documentation.
13REM This script is located in the subfolder "shims"
14REM in the project main directory.
16REM The script returns RC = 0 or another value in case of
19REM NOTOCE: The "setlocal" command jeopardizes the activation.
21REM So, all variables must be global now and the
22REM locally used ones must be undefined at the end.
25REM Check if virtual environment is already activated.
26if not "%_PYENV_VENV_DEACTIVATE%"=="" goto obsolete
27if not "%_PYENV_VENV_OLD_PATH%"=="" goto obsolete
28if not "%_PYENV_VENV_OLD_PROMPT%"=="" goto obsolete
29REM Check if "pyenv" is installed
30if "%PYENV_ROOT%"=="" goto error0
31if not exist "%PYENV_ROOT%" goto error0
32REM Parse dynamically positional arguments
35if "%~1"=="" goto scanversion
36 if "%~2"=="" goto parse1
37 set "_PYENV_VERSION=%~1"
39 REM STATUS: Input by arguments is complete.
43 REM STATUS: Input by argument gave name, but version is missing.
47REM locally inherited version, virtual global version or global version.
49set /p _PYENV_FOLDER=<"%~dp0.cwd.~"
51 REM DEBUG: echo Trying folder "%_PYENV_FOLDER%" ...
52 if exist "%_PYENV_FOLDER%\.python-version" goto breakversion1
53 REM Detect "oldest" ancestor folder by string length
54 echo %_PYENV_FOLDER% > %~dp0.len.~
55 for %%j in (%~dp0.len.~) do set /a _PYENV_STR_LEN=%%~zj - 3
56 REM DEBUG: echo Remaining string length: %_PYENV_STR_LEN%
57 if %_PYENV_STR_LEN% leq 3 goto breakversion2
58 REM Get next ancestor folder
59 for %%i in ("%_PYENV_FOLDER%\..") do set "_PYENV_FOLDER=%%~fi"
63 REM Load locally inherited version
64 set /p _PYENV_VERSION=<"%_PYENV_FOLDER%\.python-version"
65 REM DEBUG: echo Locally inherited version: %_PYENV_VERSION%
68 REM Scan for virtual global python version
69 if not exist "%PYENV_ROOT%plugins\pyenv-virtualenv\version" goto elseversion1
70 REM Load virtual global version
71 set /p _PYENV_VERSION=<"%PYENV_ROOT%plugins\pyenv-virtualenv\version"
72 REM DEBUG: echo Virtual global version: %_PYENV_VERSION%
75 REM Scan for global version
76 if not exist "%PYENV_ROOT%version" goto error1
77 REM Load global version
78 set /p _PYENV_VERSION=<"%PYENV_ROOT%version"
79 REM DEBUG: echo Global version: %_PYENV_VERSION%
82 REM Check if name is already known
83 if not "%_PYENV_NAME%"=="*" goto activate
85 REM locally inherited name or virtual global name.
87 set /p _PYENV_FOLDER=<"%~dp0.cwd.~"
89 REM DEBUG: echo Trying folder "%_PYENV_FOLDER%" ...
90 if exist "%_PYENV_FOLDER%\.python-env" goto breakname1
91 REM Detect "oldest" ancestor folder by string length
92 echo %_PYENV_FOLDER% > %~dp0.len.~
93 for %%j in (%~dp0.len.~) do set /a _PYENV_STR_LEN=%%~zj - 3
94 REM DEBUG: echo Remaining string length: %_PYENV_STR_LEN%
95 if %_PYENV_STR_LEN% leq 3 goto breakname2
96 REM Get next ancestor folder
97 for %%i in ("%_PYENV_FOLDER%\..") do set "_PYENV_FOLDER=%%~fi"
101 REM Load locally inherited name
102 set /p _PYENV_NAME=<"%_PYENV_FOLDER%\.python-env"
103 REM DEBUG: echo Locally inherited name: %_PYENV_NAME%
106 REM Scan for virtual global name
107 if not exist "%_PYENV_ROOT%\plugins\pyenv-virtualenv\env" goto elsename1
108 REM Load virtual global name
109 set /p _PYENV_NAME=<"%PYENV_ROOT%plugins\pyenv-virtualenv\env"
112 REM STATUS: Name cannot be determined
115REM Remove temprary files
116if exist "%~dp0.cwd.~" del "%~dp0.cwd.~"
117if exist "%~dp0.len.~" del "%~dp0.len.~"
118REM Check if "activate.bat" is available in expected location
119if not exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\activate.bat" goto error3
120REM Set the %PROMPT% to _PYENV_VENV_OLD_PROMPT.
121REM These is used in "deactivate.bat"
122REM to regenerate old prompt and old path.
123set "_PYENV_VENV_DEACTIVATE=%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\deactivate.bat"
124set "_PYENV_VENV_OLD_PROMPT=%PROMPT%"
125set "_PYENV_VENV_OLD_PATH=%PATH%"
126REM Set the colorized Python version prompt section in light cyan
127REM and theCWD section in light blue.
128set "PROMPT=␛[96m(%_PYENV_VERSION%)␛[0m ␛[94m%PROMPT%␛[0m"
129REM Activate the selected Python version and virtual environment
130REM DEBUG: echo Venv launcher: "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\activate.bat"
131call "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\activate.bat"
132if not "%ERRORLEVEL%"=="0" goto error4
133set "PATH=%PYENV_ROOT%plugins\pyenv-virtualenv\shims;%PATH%"S
134REM Colorize the virtual environment prompt section in yellow
135set "PROMPT=␛[93m%PROMPT%"
137REM Display error messages
140echo ␛[101mCRITICAL Environment variable "PYENV_ROOT" is incorrect.␛[0m
141echo ␛[37mINFO Check if "pyenv" for Windows is correctly installed/configured.␛[0m
145echo ␛[91mERROR Cannot find neither local inherited, virtual global nor global Python version.␛[0m
146echo ␛[37mINFO Use "pyenv install ..." and/or "pyenv global ..." or "pyenv virtualenv-props ..." to configure the version for first.␛[0m
150echo ␛[91mERROR Cannot determine the virtual envirionment name for Python version "%_PYENV_VERSION%".␛[0m
151echo ␛[37mINFO Possibly give the name as argument or set it using "pyenv virtualenv-props ...".␛[0m
155echo ␛[91mERROR Cannot find Python virtual environment "(%_PYENV_NAME%) (%_PYENV_VERSION%)".␛[0m
156echo ␛[37mINFO Check existence by calling "pyenv virtualenvs". Check if version and name are correctly given/configured.␛[0m
160echo ␛[91mERROR Failed to activate Python virtual evvironment (RC = %ERRORLEVEL%).␛[0m
161echo ␛[37mINFO Check if Python %_PYENV_VERSION% and virtual environment "%_PYENV_NAME%" are correctly installed/configured.␛[0m
165echo ␛[93mWARNING Virtual evvironment is already activated.␛[0m
182REM --- END OF CODE ----------------------------------------------------