7

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 Windows taking into consideration that there's no mechanism like shebang on Windows.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366

4 Answers4

4

File type associations are handled in the Windows registry. The virtualenv activate script would have to modify the registry keys and the deactivate script would need to restore the previous value (or risk breaking the associations).

What happens if you activate a virtualenv, open a second instance of cmd.exe, and activate a different virtualenv? Unless you deactivate them in the right order, the stored values for the registry keys would be lost.

I'm not a virtualenv developer, I'd say that the potential problems far outweigh the slight benefit.

Velociraptors
  • 2,012
  • 16
  • 22
  • The shorter answer is probably "it's complicated and painful and no one wants that feature enough to implement it". – Velociraptors Feb 03 '11 at 22:22
  • 1
    FWIW, http://www.python.org/dev/peps/pep-0397/ describes using a 'virtual' python launcher that would detect the appropriate python version. Maybe such a utility could be made to include logic suitable for virtualenv? – Macke Jun 01 '11 at 09:36
  • @Macke that proposal didn't exist when the question was asked, but it's certainly possible virtualenv could be modified work with something like that – Velociraptors Jun 01 '11 at 20:25
  • 3
    @Velociraptors In my answer to [Temporary file association for single cmd.exe session](http://stackoverflow.com/questions/5583024/) question I show how to define file type association for the duration of command line session. Virtualenv could use this to achieve what I was asking about. – Piotr Dobrogost Sep 06 '11 at 20:35
  • @Macke Good suggestion. Now, when there's [Python Launcher](http://blog.python.org/2011/07/python-launcher-for-windows_11.html) which implements PEP 397 there's a chance we could get this working. See http://bitbucket.org/vinay.sajip/pylauncher/issue/15 – Piotr Dobrogost Apr 30 '12 at 15:08
  • 2
    *The virtualenv activate script would have to modify the registry keys* Actually not. As virtualenvwrapper-win shows the solution is to associate Python files with a batch file which chooses which Python to run. – Piotr Dobrogost Nov 03 '12 at 11:16
2

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 with python.bat, a simple batch file that calls the right python.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?

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
1

The Python launcher supports custom commands. Create a py.ini file in $env:LOCALAPPDATA with a section like this:

[commands]
venvpython=C:\Path\To\Virtualenv\Scripts\python.exe

Now, you can use venvpython in the #! line of your script:

#!venvpython
import sys
print(sys.executable)
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
1

All of my Python development at present is on Linux, but I'm looking at working on Windows, which is how I found this question. My answer would be an operational one:

Instead of typing <scriptName>.py at a prompt, I always type python <scriptName>.py. If you adopt this habit, won't virtualenv execute the proper Python for you?

gomad
  • 1,029
  • 7
  • 16
  • If you adopt this habit, the answer is yes, Python's executable from active virtualenv will be executed as it's the first *python.exe* in the *PATH* environment variable. – Piotr Dobrogost Sep 17 '11 at 21:43