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

遇到问题和需求

我的电脑环境:先安装py2,然后再安装py3。我通常在工作中使用python2,如何确保两个版本共存,让代码选择要使用的版本。

[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.

遇到问题

在cmd中输入python,进入的是py2的环境,但是通过pip install模块是安装到了python3目录下

需求

随着pytho2的使用,在学习python3的新特性时,有一种方便的方法可以切换到python3。我希望您可以通过双击py文件来执行它,并且可以区分不同的版本。

[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时的环境变量

安装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\

py脚本规范

在py脚本的第一行指定python的版本,记住要双击运行python脚本,而不要通过在cmd中调用 python xx.py来执行,因为python3没有添加到环境变量,在cmd中会以python2来执行

! python2

! python3

请勿在python3目录中更改python.exe的名称,否则脚本中指定的python3版本将找不到python.exe

[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

添加这一行之后,您不会在只安装了python3的环境中出错。

[En]

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

参考资料

参考资料:python2 和 python 3和多版本如何共存_KM (netease.com)

py.exe

在安装python3.8.10时,有一个选项py Launcher。这个PY发射器是什么?安装全局“py”启动器,使启动python变得更容易

[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

注意:只有python3才有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
Type "help", "copyright", "credits" or "license" for more information.

>>>
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
Type "help", "copyright", "credits" or "license" for more information.

>>>

pip.exe

安装目录中的三个pip的MD5相同,表示它们是三个相同的文件。

[En]

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

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

正确的方法为py2或py3单独安装库,参考文章:《为不同版本python安装pip的正确做法 – 赵青青 – 博客园 (cnblogs.com)

py -2 -m pip install XXXX

py -3 -m pip install XXXX

#! python3 失效不识别

在我安装了python2x64版本之后,#!命令python3无效,打印的sys.version为python2x64。

[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)]

然后,当我卸载了版本的python2x64时,.py文件的打开方式丢失了,所以可以认为它改变了打开.py的方式。

[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

把py文件的打开方式修改为py.exe后一切都正常了,我是同时安装了python2和python3,然后查看py.exe的版本号是3.x

待验证

是否需要将python.exe放在C:Windows中才能正确标识#!巨蟒2和#!巨蟒3?

[En]

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

结论:不要求使用C:WINDOWSpython.exe,但应将.py文件的打开方式改为C:WINDOWSpy.exe,然后双击py执行即可正确识别执行时使用的是python2还是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

此可执行文件的源代码如下(编译为32位C程序):

[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

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6386/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部