Skip to content

Instantly share code, notes, and snippets.

@echiesse
Created September 20, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echiesse/020445a68198e4a78b86209f3dc4132e to your computer and use it in GitHub Desktop.
Save echiesse/020445a68198e4a78b86209f3dc4132e to your computer and use it in GitHub Desktop.
Script para ativar python virtualenvs no windows. Tenta recursivamente até a raiz.
@echo off
set venvdir=%1
for /f "delims==" %%d in ('cd') do set currentDir=%%d
call :reset-el
if not "%venvdir%" == "" (
call %venvdir%\Scripts\activate
) else (
call :attempt
)
goto :eof
:attempt
:loop
call :try-activate .
for /f "delims==" %%d in ('cd') do set envdir=%%d
if "%envdir%" == "C:\" goto :finishNotFound
if %errorlevel% equ 0 goto :finish
cd ..
goto :loop
goto :eof
:try-activate
call :reset-el
if exist %1\venv (
call %1\venv\Scripts\activate
exit /b 0
) else if exist %1\Scripts\activate (
call %1\Scripts\activate
exit /b 0
)
exit /b 1
:reset-el
exit /b 0
:finishNotFound
cd %currentDir%
echo activate: virtual env not found
exit /b
:finish
cd %currentDir%
echo activate: virtual env found in %envdir%
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment