When Python2 and Python3 are installed at the same time, how to be compatible and switch to use detailed explanation (such as pip use)

Reprinted from https://www.cnblogs.com/shabbylee/p/6792555.html


Due to historical reasons, Python has two large version branches, Python2 and Python3, and because some libraries only support a certain version branch, it is necessary to install Python2 and Python3 on the computer at the same time, so how to make the two versions of Python compatible, how to Let the script run on the corresponding Python version, this is worth summing up.

For Ubuntu 16.04 LTS version, Python2 (2.7.12) and Python3 (3.5.2) are installed at the same time by default, and the default python version is 2.7.12.

Of course you can also use python2 to call.

If you want to call python3, use python3.

 

For Windows, it's a bit more complicated. Because regardless of python2 or python3, the python executable file is called python.exe, and the version number obtained by entering python under cmd depends on which version of python path is higher in the environment variable. After all, windows is searched in order. For example, the order in the environment variable is as follows:

Then the python version under cmd is 2.7.12.

Otherwise, it is the version number of python3.

 

This brings up a problem, if you want to run a script with python2, and later you want to run another script with python3, how do you do it? Changing environment variables back and forth is obviously cumbersome.

Many methods on the Internet are relatively simple and rude. Rename two python.exes, one to python2.exe and one to python3.exe. It is possible to do this, but the way to modify the executable file is not a very good way after all.

I carefully looked up some python technical documents and found another solution that I think is better.

Borrow an argument from py to call different versions of Python .

py -2 calls python2,

py -3 calls python3.


python脚本需要python2运行时,只需在脚本前加上,然后运行py xxx.py即可。

#! python2

当python脚本需要python3运行时,只需在脚本前加上,,然后运行py xxx.py即可。

#! python3

就这么简单。

同时,这也完美解决了在pip在python2和python3共存的环境下报错,提示Fatal error in launcher: Unable to create process using '"'的问题。


以下详解转载自:http://www.cnblogs.com/an9wer/p/5564284.html

在本文中我们均使用如下的脚本(我们把它命名为script.py保存起来)来进行测验:

    1>  import sys
    2>  print(sys.version.splict()[0])  #打印出运行该程序的python版本

方法一

我们在script.py的第一行添加一行语句,以#!开头(#!被称作shebang,熟悉linux的程序员应该有所了解),改语句可以有如下四种(效果都是一样的):

1.  #!/usr/bin/env python*

2.  #!/usr/bin/python*

3.  #!/usr/local/bin/python*

4.  #!python*

其中*是指python的版本,如果此处*是2,则会从我们电脑中安装的最新版的python2来运行该脚本。例如我的电脑中同时装了python2.5和python2.6,那么它会自动选择python2.6来运行脚本。如果此处*是3,也是一样的,会选择最新一版的python3来运行。

当然,如果我们把*指定成某一明确的python版本,那么launcher会直接使用此版本来运行该脚本,前提是该版本存在。例如在脚本开头添加#!python2.6,会直接用python2.6来运行脚本。

注意:

1.  只有以上四种情况可以在脚本开头用来声明python的版本。

2.  #!后面可以加上空格。

3.  如果没有使用以上四种方法的任何一种,会默认使用电脑中安装的python2.X的最新一版来运行脚本。

方法二

我们可以不用在脚本中的第一行提前声明使用何种版本的python,而是可以在command-line中使用py语句,加上command-line argument来切换python版本。

我们这里还是以script.py为例,在command-line中输入

    py -2 script.py

其效果和在脚本开头添加#!python2是一样的,我们也开始明确某一python版本,例如我们想使用python3.3

    py -3.3 script.py

以上两个例子中的-2和-3.3即为command-line argument。

注意:

方法一和方法二同时存在时,我们以方法二为准。

例如,我在script.py开头添加#!python2.7,而在command-line中输入py -3.5 script.py,那么launcher还是会用python3.5来运行该脚本。


关于不同版本的pip package

当需要python2的pip时,只需

py -2 -m pip install xxx

当需要python3的pip时,只需

py -3 -m pip install xxx

python2和python3的pip package就这样可以完美分开了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325883195&siteId=291194637
Recommended