I am trying to write a simple batch file that will go through steps to either activate existing or create new virtual environments. Creating and activating the newly create virtual environment works as expected, but the problem arises when I try to activate an existing virtual environment.
Below is the code that I have so far:
@echo off
echo Do you want to create a new Python Virtual Environment?
set /p answer1= Enter y or n...
if /I "%answer1%"=="y" goto create
goto existing
:create
echo Batch Script to create Python Virtual Environment.
cd C:\Users\NewUser\AppData\Roaming\Python\Python310
set /p input1= Virtual Environment Name:
echo Creating virtual environment: %input%
python -m venv %input1%
echo Virtual Environment %input1% Successfully Created
echo Do you want to activate %input1%?
set /p answer2= Enter y or n...
if /I "%answer2%"=="y" goto yes
goto no
:yes
cd %input1%/Scripts
activate.bat
cls
:no
cd C:\Users\NewUser\AppData\Roaming\Python\Python310\Scripts
:existing
echo ...
echo ...
echo Select environment to activate...
cd C:\Users\NewUser\AppData\Roaming\Python\Python310
setlocal EnableDelayedExpansion
for /D %%d in (*) do (
set show=yes
for %%a in (Scripts site-packages) do if %%d == %%a set show=no
if !show! == yes echo %%d
)
set /p input2= Virtual Environment Name:
echo Activating %input2%
cd %input2%/Scripts
activate.bat