I need to set association for .py files to be executed with specific python version. But I need to make this association only for single cmd.exe session (parallel sessions should not be affected). Does Windows allow this?
I suspect the answer is no, but I’d like to see some proof before throwing out the idea to get such feature into virtualenv.
Sure you can. You were very close to answer in your comment to Jakob’s answer –
It is possible. All you have to do is to use
REG_EXPAND_SZtype of registry key and environment variable in the key’s value. For example putting%python_home%\python %1 %*as the
(Default)value ofHKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\commandkey and setting its value to
REG_EXPAND_SZmakes it possible to define what Python your Python files will be opened with. You decide by settingpython_homeenvironment variable and you can do this per command line session of course. Take a look at my answer to the question where in the registry does Windows store, with which program to open certain file types?Having said that there is now special tool for solving exactly this problem which I highly recommend. It’s called pylauncher. From the docs:
You can even skip the .py extension if you add it to the PATHEXT environment variable. See the question What environment variables will be used when calling an EXE from command line?