# 同时安装py2和py3-安装多版本python

## 遇到问题和需求

[En]

My computer environment: first install py2 and then install py3. I usually use python2 in my work, how to ensure that the two versions coexist and let the code choose the version to use.

### 需求

[En]

With pytho2 at work, there is a convenient way to switch to python3 when learning the new features of python. I hope you can execute it by double-clicking the py file, and you can distinguish between different versions.

## 解决方案

### 安装python3时的环境变量

[En]

When installing python3, * do not check * to add the installation directory to the environment variable. Do not add the following to the path of the environment variable.

1. C:\Python38
2. C:\Python38\Scripts

[En]

Do not add these directories to the path of the environment variable at the same time

1. C:\Users\zhaoqingqing01\AppData\Local\Programs\Python\Python38\
2. C:\Users\zhaoqingqing01\AppData\Local\Programs\Python\Python38\Scripts\

# ! python3

[En]

Do not change the name of python.exe in the python3 directory, otherwise the version of python3 specified in the script will not find python.exe

[En]

After adding this line, you can’t go wrong with an environment that only installs python3.

## py.exe

[En]

There is an option py launcher when installing python3.8.10. What is this py launcher? Install global “py” launcher to make it easier to start python

Py.exe程序安装在C：WINDOWSpy.exe中

[En]

The py.exe program is installed in C: Windows py.exe

• 在C:\Windows下面; 还有一个pyw.exe 是窗口版本
• py除了可以 py -2 xxx.py, 还可以 py -3.5 xxx.py
• 官方规定，打开py文件的默认方式应该是py.exe。
[En]

* officially, the default way to open py files should be py.exe.

### 通过py.exe来指定python版本

[En]

Example:

C:\Windows\System32>py -3
Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32

>>>

C:\Users\zhaoqingqing01>py -2
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32

>>>


## pip.exe

[En]

The MD5 of the three pip in the installation directory is the same, indicating that they are three identical files.

py -2 -m pip install XXXX

py -3 -m pip install XXXX

## #! python3 失效不识别

[En]

After I install the python2 x64 version, #! The command python3 is invalid, and the printed sys.version is python2 x64.

[En]

Test the code:

#! python3
# coding=utf-8

import sys
import os
print(sys.version)
os.system('pause')
#输出结果：2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]


[En]

Then when I uninstalled the version of python2 x64, the way the .py file was opened was lost, so it can be thought that it changed the way it opened .py.

• 官方默认打开py文件的方式应为py.exe，完整路径为：C：WINDOWSpy.exe
[En]

* officially, the default way to open py files should be py.exe, with the full path: C: Windows py.exe

## 待验证

[En]

Does python.exe need to be placed in C: Windows to properly identify #! python2 and #! python3?

[En]

Conclusion: C: Windows python.exe is not required, but the opening mode of the .py file should be changed to C: Windows py.exe, and then double-click py execution can correctly identify the use of python2 or python3 to execute

[En]

The source code of this exe is as follows (compiled into a 32-bit C program):

int main(int argc, char *argv[])
{
std::string s = "py ";
for (int i = 1; i < argc; ++i)
{
s += " ";
s += argv[i];
}
printf(s.c_str());
printf("\n");
system(s.c_str());
return 0;
}


Original: https://www.cnblogs.com/zhaoqingqing/p/16518839.html
Author: 赵青青
Title: 同时安装py2和py3-安装多版本python

(0)