【问题标题】:Multiple Python versions installed : how to set the default version for py.exe (Python Launcher for Windows) for CMD and for "Open with"安装了多个 Python 版本:如何为 CMD 和“打开方式”设置 py.exe(Windows 的 Python 启动器)的默认版本
【发布时间】:2023-03-14 16:27:01
【问题描述】:

简而言之:
我安装了两个版本的 Python:Python 3.6 和 Python 3.9。 我想用 Windows 的 Python Launcher 打开我所有的 .py.pyw(例如:当我双击它们时)。我希望 Windows 的 Python 启动器的默认版本是 Python 3.6,以便使用 Python 3.6 打开没有 shebang 的文件。 Python 3.9 只能用于带有 shebang #! python3.9

的文件

当我右键单击一个文件时,选择open with 并选择C:\Windows\py.exe,然后默认情况下(没有任何shebang),它是打开的Python 3.9。 当我在CMD 中输入py 时,我得到Python 3.6.5
但是如果我双击C:\Windows\py.exe,控制台是Python 3.9.5

如果我设置了open all .py' to C:\Windows\py.exe, then if they don't have a sheband they will be started with Python 3.6.5`,我该如何确定?

详情:

我只在 Python 3.6 上运行,直到我同时安装了 Python 3.9。我还没有将所有包更新到 Python 3.9,所以我希望默认 Python 为 3.6。对于需要使用 Python 3.9 的脚本,我使用 shebang #! python3.9

所以我想将Opens with... 默认设置为C:\Windows\py.exe,但如果我这样做,我的脚本会在Python 3.9.5 中打开,因此它们失败了。

我已经做了什么:

  1. 我创建了一个C:\Windows\py.ini,默认设置为python=3.6pyw 也是如此)。

  2. 在路径中,我已将C:\Users\user\AppData\Local\Programs\Python\Python36\ 移动到C:\Users\user\AppData\Local\Programs\Python\Python39 上方。

  3. 我已将另一个环境变量 PY_PYTHON 设置为 PY_PYTHON=3.6

  4. 我已经跑 assoc .py=Python

  5. Windows documentation 表示 The py.exe launcher will automatically select the most recent version of Python you've installed,所以我重新安装了 Python 3.6(在已经安装了 Python 3.9 之后),但是当我使用 C:\WINDOWS\py.exe 打开文件时,默认设置仍然设置为 Python 3.9

  6. ftype | find "Python"返回这个

    Python.ArchiveFile="C:\WINDOWS\py.exe" "%L" %*
    Python.CompiledFile="C:\WINDOWS\py.exe" "%L" %*
    Python.File="C:\WINDOWS\py.exe" "%L" %*
    Python.NoConArchiveFile="C:\WINDOWS\pyw.exe" "%L" %*
    Python.NoConFile="C:\WINDOWS\pyw.exe" "%L" %*

我使用的是 Windows 10

【问题讨论】:

  • 您目前如何启动 python?从 Windows 文件夹启动它的原因是什么?
  • which py 的结果是什么?
  • @TheLazyScripter py.exe 应该在 Windows 文件夹中。请参阅文档。
  • 更新路径后,您是打开了一个全新的 cmd 窗口,还是使用了在更改路径之前打开的那个? cmd 窗口不会根据系统控件所做的更改动态更新其环境。
  • 是的 py.exe 应该位于 Windows 文件夹中(为所有用户安装时)。但是,直接 exe 位于 appdata\local\programs\Python\MyPythonVersion 内部。并且路径变量应该链接到这里而不是 Windows 文件夹。

标签: python python-3.x cmd window launcher


【解决方案1】:

一旦您安装了 Python 的第二个版本(在本例中为 Python 3.9),如果您仍希望以前的版本(在本例中为 Python 3.6)为默认版本,则可以执行以下操作:
1) 为 CMD 中的py 命令设置默认 Python 版本

  • 转到C:\WINDOWS\并创建2个文件py.inipyw.ini

  • 这两个文件都应该包含

    [默认值]
    python=3.6

完成此操作后,如果在 CMD 中输入 py,它将打开 Python 3.6 控制台。如果您需要 Python 3.9 控制台,请键入 py -3.9。要查看哪个是默认值,请运行 py -0p 并查找 *

Installed Pythons found by py Launcher for Windows    
 -3.9-64        C:\Users\<user>\AppData\Local\Programs\Python\Python39\python.exe    
 -3.6-64        C:\Users\<user>\AppData\Local\Programs\Python\Python36\python.exe *   

不要在CMD 中使用python,始终使用带有正确参数的py [如@karl-knechtel 的评论中所示:如果您有一个活动的venv, using python instead of py` 将优先考虑venv Python,这通常是你想要的]:

py -3.9 pip install     # will install package for 3.9
py pip install     # will install package for 3.6

2) 设置文件双击打开时默认的Python版本

  • 转到C:\Users\&lt;user&gt;\AppData\Local\Programs\Python\Launcher\并创建2个文件py.inipyw.ini

  • 这两个文件都应该包含

    [默认值]
    python=3.6

  • 右键单击任何.py,选择open with,选中Always use this app...,一直向下滚动并单击more app,再次向下滚动并单击look for another app...,然后选择@987654344 @

一旦你这样做了,如果文件不包含 shebang,那么它将使用默认的 Python 版本启动,3.6。要使用 Python 3.9,请将 #!python3.9 添加到脚本的最顶部

如果您想在设置 open all with... 之前进行测试,您可以将这些添加到一些测试脚本的最顶部以查看将使用哪个版本,然后使用 open with 而不检查 use for all

  • 应该是 3.9

    #!python3.9
    导入系统
    打印(sys.version_info)
    输入(“关闭”)

  • 应该是 3.6,因为它会调用默认值

    #!python
    导入系统
    打印(sys.version_info)
    输入(“关闭”)

  • 应该是 3.6,因为它会调用默认值

    导入系统
    打印(sys.version_info)
    输入(“关闭”)

  • 应该是 3.9,因为它是最新安装的 python 3

    #!python3
    导入系统
    打印(sys.version_info)
    输入(“关闭”)

【讨论】:

  • 如果你有一个活动的venv,使用python而不是py将优先考虑venv的Python,这通常是你想要的。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 2016-02-27
  • 2017-08-05
  • 2016-04-29
  • 1970-01-01
  • 2012-11-03
  • 2020-12-13
  • 1970-01-01
相关资源
最近更新 更多