Collection of scripts and instructions for handling Python virtual environments from a central folder on Windows.
Similar to the ~/.virtualenvs folder on Linux, this is a central folder for all your virtual environments. This allows you to easily manage your virtual environments and keep them separate from your projects.
- Create a folder for your virtual environments. I use
C:\Users\{username}\virtualenvs
. - Create a system environment variable called
WORKON_HOME
and set it to the path of your virtual environments folder.setx WORKON_HOME "C:\Users\{username}\virtualenvs"
- Create a seperate folder for storing the scripts. I use
C:\Users\{username}\scripts
. - Add the scripts folder to your system path.
- There are four scripts that need to be in the scripts folder:
mkvenv.bat
@echo off setlocal enabledelayedexpansion set "venv_name=%~1" set "venv_path=%WORKON_HOME%\%venv_name%" python -m venv !venv_path! endlocal
rmvenv.bat
@echo off setlocal enabledelayedexpansion set "venv_name=%~1" set "venv_path=%WORKON_HOME%\%venv_name%" rmdir /s /q "!venv_path!" endlocal
workon.bat
@echo off set "venv_name=%~1" set "venv_path=%WORKON_HOME%\%venv_name%\Scripts\activate" call "%venv_path%"
lsvenv.bat
@echo off setlocal enabledelayedexpansion dir /b /ad "%WORKON_HOME%"
- Create a new virtual environment:
mkvenv {venv_name}
- Activate a virtual environment:
workon {venv_name}
- Deactivate a virtual environment:
deactivate
- Delete a virtual environment:
rmvenv {venv_name}
- List all virtual environments:
lsvenv
-
mkvenv.bat will use the default python version. If you want to use a specific version, you may consider using the below script instead. You can see the available versions by running
py -0
@echo off setlocal enabledelayedexpansion set "venv_name=%~1" set "python_version=%~2" if "%python_version%"=="" ( echo Please specify the Python version to use. exit /b ) set "venv_path=%WORKON_HOME%\%venv_name%" py -%python_version% -m venv !venv_path! endlocal
I've created a separate script, named mkvenv-v.bat, for this purpose because I don't always want to specify the version explicitly. If desired, you can also introduce an optional argument to the original mkvenv.bat script, with a default value pre-defined in the script.
To use it, simply run
mkvenv-v {venv_name} {python_version}
Pull requests are welcome. If you have any questions feel free to open an issue about it.