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-07-18
12REM
13REM This script is located in the subfolder "shims"
14REM in the project main 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 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%"
136goto succeed
137REM Display error messages
138:error0
139echo.
140echo ␛[101mCRITICAL Environment variable "PYENV_ROOT" is incorrect.␛[0m
141echo ␛[37mINFO Check if "pyenv" for Windows is correctly installed/configured.␛[0m
142goto fail
143:error1
144echo.
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
147goto fail
148:error2
149echo.
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
152goto fail
153:error3
154echo.
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
157goto fail
158:error4
159echo.
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
162goto fail
163:obsolete
164echo.
165echo ␛[93mWARNING Virtual evvironment is already activated.␛[0m
166goto succeed
167REM Finish
168:succeed
169set _PYENV_VERSION=
170set _PYENV_NAME=
171set _PYENV_STR_LEN=
172set _PYENV_FOLDER=
173exit /b 0
174:fail
175set _PYENV_VERSION=
176set _PYENV_NAME=
177set _PYENV_STR_LEN=
178set _PYENV_FOLDER=
179exit /b 1
180
181
182REM --- END OF CODE ----------------------------------------------------
183