重命名 python 3.7 可执行文件而不破坏内容

Rename python 3.7 executable without breaking stuff

我已将 python.exe 重命名为 python37.exe 以避免与其他版本冲突。它适用于 运行ning python,但如果我 运行 pip37.exe(位于 /Scripts),我会收到以下错误:

Fatal error in launcher: Unable to create process using '"c:\python37-32\python.exe"  "C:\Python37-32\Scripts\pip37.exe"

有没有办法让 python.exe 重命名为 python37.exe,但让所有 python 工具正常工作?

这听起来像是个坏主意。

有一些工具旨在帮助您管理这类事情。其中最好的是pyenvhttps://github.com/pyenv/pyenv

安装非常简单。这需要一点时间来适应——让你的头脑沉浸在虚拟环境中——但它最终会让你更容易使用。

例如在我的系统上,我有以下版本的 python:

pyenv versions
  system
  2.7.10
* 3.5.6 (set by /Users/.pyenv/version)
  3.5.6/envs/core4
  3.6.4
  3.6.4/envs/core5
  core4
  core5

带星号的是当前的全球版本,它将是任何默认使用的版本shell。例如,我可以使用 pyenv global 3.6.4 更改它。我还可以创建虚拟环境。例如。 core4 和 core5 是我为特定项目创建的虚拟环境。 pip install 和不同的 python 版本安装了它们各自不同的库。您可以为给定的 shell 会话激活 virtualenv,例如pyenv activate core5.

如果您正在考虑 "what on earth does this have to do with Windows",请看这里:https://duckduckgo.com/?q=Windows+Subsystem+for+Linux&atb=v93-1__&ia=web and here: http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows/

在 Windows Python 上安装 PyLauncher。您不需要虚拟环境或重命名技巧。 py.exe 位于标准 Windows 路径中,具有命令行开关以选择要使用的 Python 版本,并允许使用“shebangs”指定 Python 的哪个版本 运行 对于脚本:

py script.py          # Run the latest Python installed (or specified by PY_PYTHON environment variable).
py -2 script.py       # Run the latest Python 2 version installed.
py -3 script.py       # Run the latest Python 3 version installed.
py -2.7 script.py     # Run the specific Python version.
py -2.7-32 script.py  # Run the 32-bit specific Python version.
py -0                 # List Python versions installed.

脚本可以使用类似于 Linux:

的 shebang
#!python2
#!python3
#!python2.7
#!python2.7-32 

到运行具有特定版本的pip:

py -2.7 -m pip install ...

如果您仍然需要具有特定 Python 版本的虚拟环境,您可以指定版本(例如 -3)并使用:

py -3 -m venv <my_env_name>     # to create an virtual environment
<my_env_name>/scripts/activate  # to activate that environment

激活将虚拟环境添加到路径中,因此 python(而非 py)将在该环境中 运行。该环境中的 Scripts 目录也将添加到路径中,因此 pip 也可以直接 运行 以在该环境中安装软件包。