Advertisement
J2897

new_python_environment

Sep 1st, 2024 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.98 KB | None | 0 0
  1. @echo OFF
  2. cls
  3.  
  4. REM ===============================================================
  5. REM              Python Virtual Environment Manager
  6. REM ===============================================================
  7. REM This script helps you create and manage Python virtual environments
  8. REM with multiple Python installations on your system. It lists all
  9. REM available Python versions, allows you to select one, and creates
  10. REM a new project directory with a virtual environment using the
  11. REM selected Python version.
  12. REM
  13. REM Features:
  14. REM - Lists all installed Python versions and allows you to select one.
  15. REM - Creates a new project folder and initializes a virtual environment.
  16. REM - Automatically upgrades dependencies if supported.
  17. REM - Opens a new Python script file with the appropriate shebang line.
  18. REM - Activates the virtual environment and opens the script in Notepad++.
  19. REM
  20. REM Requirements:
  21. REM - Python installed with multiple versions (detected via 'py -0').
  22. REM - Notepad++ for editing Python scripts.
  23. REM
  24. REM How to start:
  25. REM - Simply double-click on the 'new_python_environment.bat' file.
  26. REM
  27. REM How to use:
  28. REM - Type your Python code in the Notepad++ file, save, then run by
  29. REM   typing 'python run.py' at the command line and hitting Enter.
  30. REM - Install packages, or show them with 'pip list' as normal.
  31. REM - When done, you can 'deactivate' or 'exit' to end.
  32. REM - Open the same project simply by running the script again. Just
  33. REM   enter the same name you used to create it.
  34. REM ===============================================================
  35.  
  36. REM List installed Python versions and create a selection menu
  37. echo Available Python versions:
  38. py -0 > temp_versions.txt
  39. setlocal enabledelayedexpansion
  40. set "count=0"
  41. set "choices="
  42.  
  43. REM Read the list of installed Python versions
  44. for /f "tokens=*" %%i in (temp_versions.txt) do (
  45.     if not "%%i"=="" (
  46.         set /a count+=1
  47.         echo !count!. %%i
  48.         set "version!count!=%%i"
  49.         set "choices=!choices!!count!"
  50.     )
  51. )
  52. del temp_versions.txt
  53.  
  54. REM Prompt user to select a Python version
  55. choice /C %choices% /M "Select the desired Python version by number: "
  56. set "PYTHON_VERSION=!version%ERRORLEVEL%!"
  57.  
  58. REM Extract the version number from the selected line
  59. for /f "tokens=2 delims=:- " %%i in ("!PYTHON_VERSION!") do set "PYTHON_VERSION=%%i"
  60.  
  61. REM Debugging: Display selected Python version
  62. echo Selected Python version: %PYTHON_VERSION%
  63.  
  64. REM Set the paths for the Python project
  65. set "PYTHON=%USERPROFILE%\Code\Python"
  66. set /P "PROJECT=New project name: "
  67. set "PRDIR=%PYTHON%\%PROJECT%"
  68. set "ENV=%PRDIR%\venv"
  69.  
  70. REM Check if the virtual environment already exists
  71. if exist "%ENV%" (
  72.     echo That project already exists.
  73.    REM Jump to the part where we activate the environment and open Notepad++
  74.     goto :ACTIVATE_ENV
  75. )
  76.  
  77. REM Initialize UPGRADE_DEPS to be empty
  78. set "UPGRADE_DEPS="
  79.  
  80. REM Process version numbers
  81. for /f "tokens=1,2 delims=." %%v in ("%PYTHON_VERSION%") do (
  82.     set "MAJOR=%%v"
  83.     set "MINOR=%%w"
  84.  
  85.     REM Check if the Python version should use --upgrade-deps
  86.     if !MAJOR! GTR 3 set "UPGRADE_DEPS=--upgrade-deps" else if !MAJOR!==3 (
  87.         if !MINOR! GEQ 9 set "UPGRADE_DEPS=--upgrade-deps"
  88.     )
  89. )
  90.  
  91. REM Define the command to create the virtual environment with the selected Python version.
  92. set "CREATE_VENV_CMD=py -%PYTHON_VERSION% -m venv "%ENV%" %UPGRADE_DEPS%"
  93.  
  94. REM Create the virtual environment
  95. title New Python virtual environment
  96. echo Creating the virtual environment with Python %PYTHON_VERSION% . . .
  97. call %CREATE_VENV_CMD%
  98.  
  99. REM Temporarily disable delayed expansion to write the correct line to run.py.
  100. setlocal disabledelayedexpansion
  101. echo #! python%PYTHON_VERSION%>"%PRDIR%\run.py"
  102. endlocal
  103.  
  104. :ACTIVATE_ENV
  105.  
  106. REM Activate the virtual environment
  107. title Python virtual environment
  108. cd "%PRDIR%"
  109. cls
  110. echo Activating the virtual environment...
  111. start "" /D "%PRDIR%" "C:\Program Files\Notepad++\notepad++.exe" "%PRDIR%\run.py"
  112. cmd /K "%ENV%\Scripts\activate.bat"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement