windows同时安装python2和python3

(本文仅作记录以备忘)

Python 软件基金会宣布,到 2020 年元旦,将不再为编程语言 Python 2.x 分支提供任何支持。这一天将标志着一出延续多年的戏剧的高潮:Python 从较旧的、功能较弱的、广泛使用的版本过渡到更新的、更强大的版本。然而,Python 的新版本在采用上仍然落后于它的旧版本。未来几年里,还会有很多人继续使用 Python 2 编写的程序,有些python程序使用python2开发的,比如sqlmap,与 Python 2 相比,Python 3 有着无数的技术和最终用户增强功能,但却未能永久取代 Python 2。PyPI 存储库中托管的大多数流行包都支持 Python 3(该库是可重用 Python 代码的第一站)。Python 3 现在已经成为许多 Linux 发行版默认的 Python 解释器。大多数最近出版的书籍、编码学院(coding academy)和在线课程都向初学者推荐了 Python 3。也有的程序是python3开发的就造成了运行不同的python程序需要切换不同的python版本的问题,下面是如何让python2和python3在同一windows操作系统上共存的问题。

本人再网上搜索了大量文章后,有的文章是同时安装python2和python3,但是此种方法有些复杂,而且出错了也不好解决,创建虚拟环境还需要很多命令,使用完还需要删除虚拟环境,还要改python.exe的名字,还要折腾pip,经过对比与总结,发现下面的操作方法最为实用可靠,命令也少。

我电脑之前已经安装了python2.7的环境,就不卸载了,下面到Anaconda的官网下载Python 3.7 version,windows版下载地址:

下载完成后,安装过程不再赘述,安装时要注意:

  • 1.配置环境变量。一定要勾选 Add Anaconda to my PATH environment variable,将 Anaconda 添加到环境变量。
  • 2.将 Anaconda 设为默认的 Python 环境。勾选 Register Anaconda as my default Python 3.7。

如下命令说明安装成功

1
2
C:\Users\HASEE>conda --version
conda 4.7.12

下面来配置,让两个版本的python和pip可以共存在windows上。

  • 进入python2.7的目录

将python.exe改为python2.exe

然后运行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\Users\HASEE>python2 -m pip install --upgrade pip --force-reinstall
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
Using cached https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.3.1
Uninstalling pip-19.3.1:
Successfully uninstalled pip-19.3.1
Successfully installed pip-19.3.1

C:\Users\HASEE>python -m pip install --upgrade pip --force-reinstall
Collecting pip
Using cached https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.3.1
Uninstalling pip-19.3.1:
Successfully uninstalled pip-19.3.1
Successfully installed pip-19.3.1

然后测试是否成功共存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
C:\Users\HASEE>pip2 --version
pip 19.3.1 from d:\python27\lib\site-packages\pip (python 2.7)

C:\Users\HASEE>pip3 --version
pip 19.3.1 from d:\anaconda3\lib\site-packages\pip (python 3.7)

C:\Users\HASEE>python2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\HASEE>python
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>>

测试成功,两个版本的python成功共存。

使用此种方法配置,不但可以共存还可以使用conda在虚拟环境中配置python

如果报以下错误:

1
2
C:\Users\HASEE>virtualenv
Fatal error in launcher: Unable to create process using '"d:\python27\python.exe" "D:\Python27\Scripts\virtualenv.exe"

运行如下命令即可解决:

1
2
3
4
5
6
C:\Users\HASEE>python -m pip install --upgrade virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/62/77/6a86ef945ad39aae34aed4cc1ae4a2f941b9870917a974ed7c5b6f137188/virtualenv-16.7.8-py2.py3-none-any.whl (3.4MB)
|████████████████████████████████| 3.4MB 467kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.8

成功解决

1
2
C:\Users\HASEE>virtualenv --version
16.7.8

Anaconda使用参考:

用 Anaconda 完美解决 Python2 和 python3 共存问题

长舒一口气,真是太兴奋了!!!

THE END