Skip to content

Instantly share code, notes, and snippets.

@sio
Created September 19, 2017 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sio/fbc46ae41607b206ce9099dc8485df34 to your computer and use it in GitHub Desktop.
Save sio/fbc46ae41607b206ce9099dc8485df34 to your computer and use it in GitHub Desktop.
Set up temporary Python virtual environment on Windows
@echo off
REM This script sets up Python virtual environment in temporary directory
REM and runs a new cmd session within. When that cmd session ends, the script
REM deletes virtual environment.
REM
REM Copyright 2017 Vitaly Potyarkin
REM
REM Licensed under the Apache License, Version 2.0
REM http://www.apache.org/licenses/LICENSE-2.0
SETLOCAL
if [%1] == [] (
set NAME=temporary_venv
) else (
set NAME=%1
)
set VENV=%TEMP%\%NAME%
set PYTHON=python
echo Creating new virtual environment for Python in %VENV%
rmdir /s /q %VENV% 2>NUL
%PYTHON% -m venv %VENV% || echo -- venv failed && exit /b
call %VENV%\Scripts\activate.bat || echo -- activate failed && exit /b
python -m pip install --upgrade pip || echo -- upgrading pip failed && exit /b
echo Starting new shell session...
echo Virtual environment will be cleared upon exit
cmd
echo Deleting virtual environment...
call deactivate
rmdir /s /q %VENV%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment