Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

python - Temporary file association for single cmd.exe session

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure you can. You were very close to answer in your comment to Jakob's answer -

If it is possible to change file association with environment variables - it will help, but it doesn't seem possible.

It is possible. All you have to do is to use REG_EXPAND_SZ type of registry key and environment variable in the key's value. For example putting
%python_home%python %1 %*
as the (Default) value of
HKEY_LOCAL_MACHINESOFTWAREClassesPython.Fileshellopencommand
key and setting its value to REG_EXPAND_SZ makes it possible to define what Python your Python files will be opened with. You decide by setting python_home environment 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:

PEP 397 compatible launcher for Python under Windows. See http://www.python.org/dev/peps/pep-0397/ for PEP, http://www.red-dove.com/screencasts/launcher/launcher.html for screencast

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...