Python 2, Python 3 同时安装,容易出现共存问题,尤其是 pip 的使用。本文内容参考了很多网上资料,在此不一一列出。

Python 2, Python 3, PyCharm 下载

1 Python 下载地址 ,找到合适版本,本人下载 64 位版本
Windows 下 Python 2, Python 3, PyCharm 安装
Windows 下 Python 2, Python 3, PyCharm 安装
2 PyCharm 下载地址 ,下载社区版即可
Windows 下 Python 2, Python 3, PyCharm 安装

Python 2 与 Python 3 共存

1. 运行不同版本 Python

安装 Python 3.3 以上的版本时,Python 会在 C:\Windows 文件夹下安装启动器py.exe。可以指定 Python 运行版本
py -2   运行 Python 2
py -3   运行 Python 3

双击脚本调用的就是这个程序:

Windows 下 Python 2, Python 3, PyCharm 安装

2. 指定 pip 安装 package

py -2 -m pip install package_name 指定 Python 2 安装 package
py -3 -m pip install package_name 指定 Python 3 安装 package

Python 3 安装

Python 3 安装比较简单,一般默认安装就行,注意记得将环境变量项勾选上(下图中蓝色标记),然后点击 Install Now 即可

Windows 下 Python 2, Python 3, PyCharm 安装

安装完成后,可以到命令提示符(cmd)界面进行测试,两个命令均可以,直接输入 python 或者 py -3, 能够出现图示中的版本信息,表示安装成功。

Windows 下 Python 2, Python 3, PyCharm 安装

Python 2 安装

  1. 点击下载好的 msi 文件进行 Python 2 的安装
    Windows 下 Python 2, Python 3, PyCharm 安装

  2. 选择默认路径
    Windows 下 Python 2, Python 3, PyCharm 安装

  3. 记得勾选上环境变量,也可以安装完成后自己手动添加
    Windows 下 Python 2, Python 3, PyCharm 安装

  4. 安装完成
    Windows 下 Python 2, Python 3, PyCharm 安装

  5. 检查是否安装成功。运行 python 时默认为 Python 3, 可以运行 py -2 运行 Python 2
    Windows 下 Python 2, Python 3, PyCharm 安装
    上图说明 Python 2 安装成功!

PyCharm 安装

  1. 点击 exe 开始安装
    Windows 下 Python 2, Python 3, PyCharm 安装

  2. 指定安装目录,可以选择默认安装。此处指定至 D 盘根目录下
    Windows 下 Python 2, Python 3, PyCharm 安装

  3. 记得勾选上图中两个选项
    Windows 下 Python 2, Python 3, PyCharm 安装

  4. 等待安装
    Windows 下 Python 2, Python 3, PyCharm 安装

  5. 安装完成
    Windows 下 Python 2, Python 3, PyCharm 安装

PyCharm 中测试不同版本 Python

PyCharm 本身可以添加 Python package , 更方便的方式是使用 pip 在 cmd 中添加,我们先在 Python 2 中安装如下 packages,这是 scipy , numpy 包安装时,官方提供的一系列包,具体内容请参考这里

py -2 -m pip install –user numpy scipy matplotlib ipython jupyter pandas sympy nose

如果不会报错,就安装成功,如下图,有时候网速不好也可能报错
Windows 下 Python 2, Python 3, PyCharm 安装

我们可以测试一下 numpy 包,返回该包路径(蓝色横线标记),说明安装成功
Windows 下 Python 2, Python 3, PyCharm 安装

由于刚才仅仅在 Python 2 下安装了 numpy 等包,在 Python 3 下调用将会出错
Windows 下 Python 2, Python 3, PyCharm 安装

用 pip 安装好了 packages 后,可以在 PyCharm 中测试,启动 PyCharm,首次启动时,会有导入偏好设置,这里选择不导入
Windows 下 Python 2, Python 3, PyCharm 安装

接受那个 Policy 就可以
Windows 下 Python 2, Python 3, PyCharm 安装

选择一个主题,然后跳过剩余的设置
Windows 下 Python 2, Python 3, PyCharm 安装

新建工程
Windows 下 Python 2, Python 3, PyCharm 安装

接下来需要注意,可以在 Location 中选择工程保存的位置,选择 Python 2 作为解释器(base interpreter),特别注意,需要在    Inherit global site-package    前打钩,这样才可以对 cmd 中 pip 安装的包可见
Windows 下 Python 2, Python 3, PyCharm 安装

在新建的工程中,添加一个 Main.py 文件
Windows 下 Python 2, Python 3, PyCharm 安装

我们测试一下 numpy 这个包,输出其路径,其结果跟 cmd 中输出是一致的,说明 PyCharm 中使用 Python 2 解释器,并调用 pip 安装的包是成功的
Windows 下 Python 2, Python 3, PyCharm 安装

我们可以在 PyCharm 中查看 pip 安装的包,File –> Settings…. –> Project: Test –> Project Interpreter, 其中,Test 是我们新建的工程名称
Windows 下 Python 2, Python 3, PyCharm 安装

现在,我们重新建立一个 Python 3 的工程,看一下相关的包。记得选择 Python 3 解释器,同样,特别注意,需要在    Inherit global site-package    前打钩,这样才可以对 cmd 中 pip 安装的包可见
Windows 下 Python 2, Python 3, PyCharm 安装

同样,新建一个 Main.py 的文件,并打印一个结果,说明 PyCharm 中使用 Python 3 解释器也运行成功。
Windows 下 Python 2, Python 3, PyCharm 安装

由于我们暂时并没有在 Python 3 下安装额外的包,在 PyCharm 里应该看不到包。下图表明,并没有我们安装的 numpy 等包,说明   py -2 -m pip install package_name   安装的包,不会混杂到 Python 3 中的包路径里。
Windows 下 Python 2, Python 3, PyCharm 安装

至此,所有内容成功完成。☺

相关文章:

  • 2021-12-19
  • 2021-12-04
  • 2022-12-23
  • 2021-10-04
  • 2021-06-18
  • 2021-10-16
  • 2021-11-12
  • 2018-07-15
猜你喜欢
  • 2021-11-02
  • 2021-12-03
  • 2021-07-24
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2021-10-09
相关资源
相似解决方案