简体   繁体   English

在 Windows 10 cmd 中在不同 Python 版本之间切换

[英]Switch between different Python versions in Windows 10 cmd

I don't like the other post .我不喜欢另一个帖子 because it involves renaming python executables.因为它涉及重命名 python 可执行文件。

Here's my discoveries.这是我的发现。

Step 1. Go to System Properties .步骤 1. 转到系统属性 Click on Environment Variables点击Environment Variables

Step 2. Add new variables, such as PYTHON_27_HOME步骤 2. 添加新变量,例如PYTHON_27_HOME

  • PYTHON_27_HOME : %ProgramFiles%\\Python27\\ PYTHON_27_HOME : %ProgramFiles%\\Python27\\
  • PYTHON_36_HOME : %ProgramFiles%\\Python36\\ PYTHON_36_HOME : %ProgramFiles%\\Python36\\
  • PYTHON_HOME : %PYTHON_27_HOME% PYTHON_HOME%PYTHON_27_HOME%

In my case, PYTHON_27_HOME (Python 2.7) is pointing to C:\\Program Files\\Python27\\ .就我而言, PYTHON_27_HOME (Python 2.7) 指向C:\\Program Files\\Python27\\ You can replace this with your own path to python .您可以将其替换为您自己的python路径。 %PYTHON_HOME% has a default value pointing to %PYTHON_27_HOME% , which is the path for Python 2.7. %PYTHON_HOME%的默认值指向%PYTHON_27_HOME% ,这是 Python 2.7 的路径。 That's my preference, feel free to adjust accordingly.这是我的偏好,请随时进行相应调整。 Be aware that there're 32-bit and 64-bit python.请注意,有 32 位和 64 位 python。 Please use %PROGRAMFILES% for path to C:\\Program Files and %PROGRAMFILES(X86)% for path to C:\\Program Files (x86) .请使用%PROGRAMFILES%作为C:\\Program Files路径,使用%PROGRAMFILES% %PROGRAMFILES(X86)%作为C:\\Program Files (x86)路径。

Step 3. Select PATH and click on Edit .步骤 3. 选择PATH并点击Edit PATH小路

Step 4. Click on New and add %PYTHON_HOME% .步骤 4. 单击New并添加%PYTHON_HOME% %PYTHON_HOME% will be added to PATH automatically every time you launch a command prompt.每次启动命令提示符时, %PYTHON_HOME%都会自动添加到PATH


In order to switch between different versions of python in cmd, here's the trick.为了在cmd中在不同版本的python之间切换,这里有诀窍。

Step 5. I created a batch file with第 5 步。我创建了一个批处理文件

@echo off
:: Switch Python version
DOSKEY python27=SET PATH=%PYTHON_27_HOME%;%PATH%
DOSKEY python36=SET PATH=%PYTHON_36_HOME%;%PATH%

Basically, it disables echo and creates two alias.基本上,它禁用 echo 并创建两个别名。 In batch file any string after :: is the comments.在批处理文件中::之后的任何字符串都是注释。 Every time, python27 or python36 is called, it re-exports %PATH% with the new Python path.每次, python27python36被调用,它转口%PATH%与新的Python路径。 Save it as profile.bat .将其另存为profile.bat You can name it whatever you want.您可以随意命名。

Step 6. Search for regedit (Registry Editor).步骤 6. 搜索regedit (注册表编辑器)。 Click on Edit > New > String Value .单击“ Edit >“ New >“ String Value Give AutoRun as the Value name and %USERPROFILE%\\profile.bat as the Value data .AutoRun作为Value name ,将%USERPROFILE%\\profile.bat作为Value data Here, please put your actual path value to the profile.bat we just created.在这里,请将您的实际路径值放入我们刚刚创建的profile.bat So, whenever a command prompt is opened, it automatically loads profile.bat , which creates those two alias in the script.因此,无论何时打开命令提示符,它都会自动加载profile.bat ,这会在脚本中创建这两个别名。

Step 7. Close any command prompt you're using or just open a new command prompt.步骤 7. 关闭您正在使用的任何命令提示符或只打开一个新的命令提示符。 This is because your changes will not affect opened cmd window.这是因为您的更改不会影响打开的 cmd 窗口。 Environment changes only happens to new CMD.环境变化只发生在新的 CMD 上。

Step 8. Verify your results here .步骤 8.在此处验证您的结果。

If you're using different Java versions, same trick applies, too.如果您使用不同的 Java 版本,同样的技巧也适用。 Find my javac environment setting here .在这里找到我的javac环境设置。

I think the easiest way to support various versions of Python, as well as other languages, is the asdf version manager.我认为支持各种版本的 Python 以及其他语言的最简单方法是 asdf 版本管理器。 It allows you to set a version of Python globally, as well as in each project folder.它允许您在全局以及每个项目文件夹中设置 Python 版本。 This means that you can set your Python version to dynamically change based upon the folder you're working in.这意味着您可以将 Python 版本设置为根据您工作的文件夹动态更改。

asdf version manager asdf 版本管理器

I haven't used Windows for almost 20 years, but I've heard that Windows 10 sports an Ubuntu-based subsystem for Linux.我已经将近 20 年没有使用 Windows,但我听说 Windows 10 为 Linux 提供了一个基于 Ubuntu 的子系统。 I don't know if asdf will work with that, but it is worth a try.我不知道 asdf 是否可以使用它,但值得一试。 Just use the instructions for setting asdf up with bash.只需使用使用 bash 设置 asdf 的说明即可。

TL;DR: Use a symbolic link to youre current version and put that on your path instead. TL;DR:使用指向您当前版本的符号链接并将其放在您的路径上。

Linux has the alternatives command to switch versions globally, but I do this: Linux 有替代命令来全局切换版本,但我这样做:

  1. Install Python variants under C:\\Python\\Python38 , C:\\Python\\Python36 etcC:\\Python\\Python38C:\\Python\\Python36等下安装 Python 变体
  2. Set your PATH to include C:\\Python\\Current;C:\\Python\\Current\\Scripts设置你的 PATH 以包含C:\\Python\\Current;C:\\Python\\Current\\Scripts
  3. Have a batch file like this to switch versions:有一个这样的批处理文件来切换版本:
    @echo off
    echo 1. Python 3.8
    echo 2. Python 3.6   
    set /p ver="Enter Version: "
    
    if [%ver%]==[1] (
        SET FOLDER=C:\Python\Python38
    ) ELSE if [%ver%]==[2] (
        SET FOLDER=C:\Python\Python36
    ) ELSE (
        GOTO end
    )
    
    if exist C:\Python\Current\nul (
        rmdir C:\Python\Current
    )
    
    MKLINK /D C:\Python\Current %FOLDER%
    
    :end

The only downside is that MKLINK (and hence the batch file) requires elevated privilages唯一的缺点是 MKLINK(以及批处理文件)需要提升权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM