单个 cmd.exe 会话的临时文件关联 [英] Temporary file association for single cmd.exe session

查看:41
本文介绍了单个 cmd.exe 会话的临时文件关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为要使用特定 python 版本执行的 .py 文件设置关联.但是我只需要为单个 cmd.exe 会话建立这种关联(并行会话不应该受到影响).Windows 允许这样做吗?

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?

我怀疑答案是否定的,但我想在放弃将此类功能引入 virtualenv 的想法之前先看看一些证据.

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.

推荐答案

当然可以.您在对 Jakob 回答的评论中非常接近回答 -

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.

这是可能的.您所要做的就是在键的值中使用 REG_EXPAND_SZ 类型的注册表项和环境变量.例如把
%python_home%\python %1 %*
作为
(Default)HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command
键并将其值设置为 REG_EXPAND_SZ 可以定义您的 Python 文件将使用什么 Python 打开.您可以通过设置 python_home 环境变量来决定,当然您可以在每个命令行会话中执行此操作.看看我对Windows 存储在注册表中的哪个位置,使用哪个程序打开某些类型的文件?

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_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command
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?

话虽如此,我强烈推荐现在有专门的工具来解决这个问题.它被称为 pylauncher.来自文档:

Having said that there is now special tool for solving exactly this problem which I highly recommend. It's called pylauncher. From the docs:

适用于 Windows 下 Python 的 PEP 397 兼容启动器.看http://www.python.org/dev/peps/pep-0397/ 对于 PEP,http://www.red-dove.com/screencasts/launcher/launcher.html 用于截屏

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

如果将 .py 扩展名添加到 PATHEXT 环境变量中,您甚至可以跳过它.请参阅问题从命令行调用 EXE 时将使用哪些环境变量?

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?

这篇关于单个 cmd.exe 会话的临时文件关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆