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
activate.bat
Go to the documentation of this file.
1@echo off
2REM Windows Command Line Script (Batch) to activate virtual environment.
3REM
4REM Dependencies:
5REM * pyenv
6REM * pyenv-virtualenv-init
7REM
8REM © 2025 Michael Paul Korthals. All rights reserved.
9REM For legal details see documentation.
10REM
11REM 2025-08-04
12REM
13REM This script is located in the subfolder "shims"
14REM in the plugin root directory.
15REM
16REM The script returns RC = 0 or another value in case of
17REM error.
18REM
19REM NOTOCE: The "setlocal" command jeopardizes the activation.
20REM Do not use it,
21REM So, all variables must be global now and the
22REM locally used ones must be undefined at the end.
23REM
24
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
33set "_PYENV_VERSION=*"
34set "_PYENV_NAME=*"
35if "%~1"=="" goto scanversion
36 if "%~2"=="" goto parse1
37 set "_PYENV_VERSION=%~1"
38 set "_PYENV_NAME=%~2"
39 REM STATUS: Input by arguments is complete.
40 goto activate
41 :parse1
42 set "_PYENV_NAME=%~1"
43 REM STATUS: Input by argument gave name, but version is missing.
44 goto scanversion
45:scanversion
46REM Scan for
47REM locally inherited version, virtual global version or global version.
48cd > "%~dp0.cwd.~"
49set /p _PYENV_FOLDER=<"%~dp0.cwd.~"
50:loopversion1
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"
60 REM Continue loop
61 goto loopversion1
62:breakversion1
63 REM Load locally inherited version
64 set /p _PYENV_VERSION=<"%_PYENV_FOLDER%\.python-version"
65 REM DEBUG: echo Locally inherited version: %_PYENV_VERSION%
66 goto scanname
67:breakversion2
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%
73 goto scanname
74 :elseversion1
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%
80 goto scanname
81:scanname
82 REM Check if name is already known
83 if not "%_PYENV_NAME%"=="*" goto activate
84 REM Scan for
85 REM locally inherited name or virtual global name.
86 cd > "%~dp0.cwd.~"
87 set /p _PYENV_FOLDER=<"%~dp0.cwd.~"
88 :loopname1
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"
98 REM Continue loop
99 goto loopname1
100 :breakname1
101 REM Load locally inherited name
102 set /p _PYENV_NAME=<"%_PYENV_FOLDER%\.python-env"
103 REM DEBUG: echo Locally inherited name: %_PYENV_NAME%
104 goto activate
105 :breakname2
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"
110 goto activate
111 :elsename1
112 REM STATUS: Name cannot be determined
113 goto error2
114:activate
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
127goto error4
128: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
148echo.
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
153goto succeed
154REM Display error messages
155:error0
156echo.
157echo ␛[101mCRITICAL Environment variable "PYENV_ROOT" is incorrect.␛[0m
158echo ␛[37mINFO Check if "pyenv" for Windows is correctly installed/configured.␛[0m
159goto fail
160:error1
161echo.
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
164goto fail
165:error2
166echo.
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
169goto fail
170:error3
171echo.
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
174goto fail
175:error4
176echo.
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
184goto fail
185:error5
186echo.
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
189goto fail
190:obsolete
191echo.
192echo ␛[93mWARNING Virtual environment is already activated.␛[0m
193goto succeed
194REM Finish
195:succeed
196set _PYENV_VERSION=
197set _PYENV_NAME=
198set _PYENV_STR_LEN=
199set _PYENV_FOLDER=
200exit /b 0
201:fail
202set _PYENV_VERSION=
203set _PYENV_NAME=
204set _PYENV_STR_LEN=
205set _PYENV_FOLDER=
206exit /b 1
207
208
209REM --- END OF CODE ----------------------------------------------------
210