This seems like a reasonable request, which seems like it could be simply solved by looking for a VIRTUAL_ENV environment variable, which (if it contains a python.exe in the appropriate subfolder) can be used as the executable to launch.
I will ask Mark Hammond if we can update the PEP to reflect this.
I just had a thought: if you are talking about scripts installed into the virtual environment, then they will already use the virtual environment's Python, since correct shebang lines should be written into the scripts during installation into a virtual env.
Can you clarify the use case a little more? For example, are you asking that with a virtual env activated, ANY Python script should run with that virtual env's Python? That doesn't seem right. ISTM the shebang should generally take precedence.
which seems like it could be simply solved by looking for a VIRTUAL_ENV environment variable, which (if it contains a python.exe in the appropriate subfolder) can be used as the executable to launch.
Right. However when I checked this today this variable remains after deactivating virtualenv (virtualenv 188.8.131.52) which seems to be a bug.
For example, are you asking that with a virtual env activated, ANY Python script should run with that virtual env's Python? That doesn't seem right. ISTM the shebang should generally take precedence.
Any script using virtual command without version qualifier or without shebang is a good start. If we add checking version of virtualenv's Python we could support also virtual commands with version qualifiers. With this reservation I hope it does seem right :)
It seems like launcher is the perfect tool to remove dichotomy between the behavior we get when running Python script with python script and with script.py
I discussed this with Mark Hammond. Although your request is not unreasonable, it seems that it would only benefit users who want to run a script foo.py which is not installed in a virtualenv, but with that virtualenv's Python, without typing "python foo.py". You would need to do this on POSIX platforms anyway, and the launcher is aiming to regularise behaviour between platforms as far as practicable.
For any script actually installed in a virtualenv, the shebang line is set (by easy_install etc.) to point to the virtualenv's Python. So just typing foo.py should work with the virtualenv activated, because its Scripts folder is on the path, the .py extension invokes the launcher, the launcher reads the shebang and invokes the virtualenv's Python.
If the env is not activated, you need to invoke /path/to/env/Scripts/foo.py, but that's just a PATH issue, not a launcher issue.
There's also the situation that the VIRTUAL_ENV won't be set when you double click on a file in Explorer, so you would get different behaviour. Plus the outstanding issue which you posted to the virtualenv tracker.
All in all, it's probably not worth making this change, because for regularly used scripts, they can be installed in a virtualenv, and then the launcher logic should work as is (though if you find it doesn't, please post a separate issue, as long as the offending script has a shebang line pointing to the env's Python). For occasionally used scripts, it should be OK to type "python " in front of the script name, or you can add in a suitable shebang line to the script.