What is the reason virtualenv does not associate .py(w)
files with virtualenv\'s version of Python executables? This seems like an ideal task for virtualenv on
virtualenvwrapper-win does associate Python files with currently active virtualenv:
Note that the batch script
pyassoc
requires an elevated command prompt or that UAC is disabled. This script associates .py files withpython.bat
, a simple batch file that calls the rightpython.exe
based on whether you have an active virtualenv. This allows you to call python scripts from the command line and have the right python interpreter invoked. Take a look at the source -- it's incredibly simple but the best way I've found to handle conditional association of a file extension.
python.bat looks like this
@echo off
if defined PYTHONHOME (
goto MAIN
)
FOR /F "tokens=*" %%i in ('whereis.bat python.exe') do set PYTHONHOME=%%~dpi
SET PYTHONHOME=%PYTHONHOME:~0,-1%
:MAIN
SETLOCAL EnableDelayedExpansion
if defined VIRTUAL_ENV (
set PY="%VIRTUAL_ENV%\Scripts\python.exe"
) else (
set PY="%PYTHONHOME%\python.exe"
)
ENDLOCAL & %PY% %*
:END
UPDATE
Now it's possible – see How to associate Python scripts with active virtualenv?