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 plugin root 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 temporary 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 Check if a 'pip' executable is available in expected location
121if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.exe" goto activate1
122if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.bat" goto activate1
123if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.cmd" goto activate1
124if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.vbs" goto activate1
125if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.py" goto activate1
126if exist "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip*.pyw" goto activate1
129REM Set the %PROMPT% to _PYENV_VENV_OLD_PROMPT.
130REM These is used in "deactivate.bat"
131REM to regenerate old prompt and old path.
132set "_PYENV_VENV_DEACTIVATE=%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\deactivate.bat"
133set "_PYENV_VENV_OLD_PROMPT=%PROMPT%"
134set "_PYENV_VENV_OLD_PATH=%PATH%"
135REM Set the colorized Python version prompt section in light cyan
136REM and theCWD section in light blue.
137set "PROMPT=␛[96m(%_PYENV_VERSION%)␛[0m ␛[94m%PROMPT%␛[0m"
138REM Activate the selected Python version and virtual environment
139REM DEBUG: echo Venv launcher: "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\activate.bat"
140call "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\activate.bat"
141if not "%ERRORLEVEL%"=="0" goto error5
142set "PATH=%PYENV_ROOT%plugins\pyenv-virtualenv\shims;%PATH%"S
143REM Colorize the virtual environment prompt section in yellow
144set "PROMPT=␛[93m%PROMPT%"
145REM Check if package 'virtualenv' is installed
146python -c "import virtualenv" 1>nul 2>nul
147if "%ERRORLEVEL%"=="0" goto succeed
149echo ␛[93mWARNING Package "virtualenv" is not installed in this virtual environment.
150echo ␛[95mNOTICE So, calling "pyenv-virtualenv" commands with this activated virtual environment could fail.␛[0m
151echo ␛[37mINFO To avoid this problem, run "pip install virtualenv" now.␛[0m
152echo ␛[37mINFO Otherwise, call "deactivate" before calling "pyenv-virtualenv" commands.␛[0m
154REM Display error messages
157echo ␛[101mCRITICAL Environment variable "PYENV_ROOT" is incorrect.␛[0m
158echo ␛[37mINFO Check if "pyenv" for Windows is correctly installed/configured.␛[0m
162echo ␛[91mERROR Cannot find neither local inherited, virtual global nor global Python version.␛[0m
163echo ␛[37mINFO Use "pyenv install ..." and/or "pyenv global ..." or "pyenv virtualenv-props ..." to configure the version for first.␛[0m
167echo ␛[91mERROR Cannot determine the virtual envirionment name for Python version "%_PYENV_VERSION%".␛[0m
168echo ␛[37mINFO Possibly give the name as argument or set it using "pyenv virtualenv-props ...".␛[0m
172echo ␛[91mERROR Cannot find Python virtual environment "(%_PYENV_NAME%) (%_PYENV_VERSION%)".␛[0m
173echo ␛[37mINFO Check existence by calling "pyenv virtualenvs". Check if version and name are correctly given/configured.␛[0m
177echo ␛[91mERROR Cannot find 'pip' executable in virtual environment.␛[0m
178echo ␛[37mINFO See "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts".␛[0m
179echo ␛[95mNOTICE You would be unable to install packages.␛[0m
180echo ␛[37mINFO Manually install/repair 'pip'. Then try again.␛[0m
181echo ␛[37mINFO Or, execute this command to permanently bypass this error:␛[0m
182echo ␛[37mINFO echo @echo 'pip' not found. ^& exit /b 2 ^> "%PYENV_ROOT%versions\%_PYENV_VERSION%\envs\%_PYENV_NAME%\Scripts\pip.bat"␛[0m
183echo ␛[37mINFO Or, migrate to Python version 3.4+ to annihilate this problem at root.␛[0m
187echo ␛[91mERROR Failed to activate Python virtual environment (RC = %ERRORLEVEL%).␛[0m
188echo ␛[37mINFO Check if Python %_PYENV_VERSION% and virtual environment "%_PYENV_NAME%" are correctly installed/configured.␛[0m
192echo ␛[93mWARNING Virtual environment is already activated.␛[0m
209REM --- END OF CODE ----------------------------------------------------