File size: 4,506 Bytes
68964c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@echo off
REM Define the name of the virtual environment folder in the root directory
set ROOT_DIR=%~dp0..
set VENV_NAME=%ROOT_DIR%\venv

REM Check if the virtual environment already exists
if exist %VENV_NAME% (
    echo ============================================================
    echo Virtual environment already exists in:
    echo %VENV_NAME%
    echo ============================================================
    echo Please delete the existing virtual environment if you want to recreate it.
    echo ============================================================
    pause
    exit /b 1
)

REM Find all available Python executables
echo ============================================================
echo Searching for available Python versions...
echo ============================================================
setlocal enabledelayedexpansion
set count=1
for /f "delims=" %%P in ('where python') do (
    echo !count!. %%P
    set "PYTHON_!count!=%%P"
    set /a count+=1
)

REM Check if any Python executables were found
if %count%==1 (
    echo ============================================================
    echo No Python executables found on the system.
    echo Ensure Python is installed and added to PATH.
    echo ============================================================
    pause
    exit /b 1
)

REM Prompt the user to choose a Python version
set /a MAX_CHOICE=%count%-1
echo ============================================================
set /p CHOICE="Enter the number corresponding to the Python version you want to use: "

REM Validate the user's choice
if %CHOICE% lss 1 if %CHOICE% gtr %MAX_CHOICE% (
    echo ============================================================
    echo Invalid choice. Exiting.
    echo ============================================================
    pause
    exit /b 1
)

REM Get the selected Python executable
for /f "tokens=2 delims==" %%P in ('set PYTHON_%CHOICE%') do set SELECTED_PYTHON=%%P

REM Check if the selected Python executable is valid
%SELECTED_PYTHON% --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ============================================================
    echo The selected Python executable is not valid or not found.
    echo ============================================================
    pause
    exit /b 1
)

REM Create the virtual environment
echo ============================================================
echo Creating virtual environment in root directory using %SELECTED_PYTHON%...
echo ============================================================
%SELECTED_PYTHON% -m venv %VENV_NAME%

REM Check if the virtual environment was created successfully
if exist %VENV_NAME% (
    echo ============================================================
    echo Virtual environment created successfully in:
    echo %VENV_NAME%
    echo ============================================================
) else (
    echo ============================================================
    echo Failed to create virtual environment.
    echo Please check for errors and try again.
    echo ============================================================
    pause
    exit /b 1
)

REM Activate the virtual environment for testing
echo ============================================================
echo Activating virtual environment for testing...
echo ============================================================
call %VENV_NAME%\Scripts\activate.bat

REM Confirm activation
if "%VIRTUAL_ENV%"=="" (
    echo ============================================================
    echo Failed to activate virtual environment.
    echo Please check for errors and try again.
    echo ============================================================
    pause
    exit /b 1
) else (
    echo ============================================================
    echo Virtual environment activated successfully for testing.
    echo Running a test command inside the virtual environment...
    echo ============================================================
    python --version
    echo ============================================================
    echo Deactivating virtual environment after testing...
    echo ============================================================
    deactivate
)

REM Allow the user to read the success or failure message
echo ============================================================
echo Process completed. Press any key to exit.
echo ============================================================
pause