Instantly share code, notes, and snippets.

@sio sio/venv_temp.bat
Created Sep 19, 2017

Embed
What would you like to do?
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