Tutorial Example

Install and Use Both Python 2 and Python 3 in Windows with Anaconda – Python Tutorial

Python 2 and Python 3 is different, if you want to install and use both of them to programming, we will recommend anaconda to implement. In this tutorial, we will introduce how to do.

Preliminaries

Install Anaconda in windows, you can download it at here.

Anaconda Python/R Distribution

Both anaconda python 3.7 and python 2.7 can be selected and installed on windows system, you select one of them.

After you have installed anaconda, such as 2.7 version, you can set up python 2.7 and python 3.5 environment.

Set up python 2.7 environment

open cmd 

conda create --name py2 python=2.7

After you have set up python 2.7, you can know this environment

conda info --envs

If you want to use python 2.7, such as install some library, you shoud activate it.

Activate python 2.7

activate py2

if you are using linux or mac, you can use:

source activate py2

Then you can start to progam based on python 2.7.

If you do not want to use python 2.7, you can deactivate it.

Deactivate python 2.7

deactivate py2

on linx or mac, you can use:

source deactivate

Set up python 3.5

Similar to set up python 2.7, we can also set up a python 3.5 environment.

conda create --name py3 python=3.5

Then we can activate it.

activate py3

on linux or mac.

source activate py3

If you do not want to use python 3.5, you also can deactivate it like python 2.7.

Then we have installed python 2.7 and 3.5 in our computer. However, how to switch them? Here we will use Eclipse+PyDev for an example.

How to swith python version in Eclipse?

Open Python Interpreters

Add pyhon 2.7 and python 3.5

Then you can get this result:

We write an example to test python version and path.

import sys
print (sys.executable)
print(sys.version)

If path.exe is above, you will get result:

C:\ProgramData\Miniconda2\envs\py3\python.exe
3.5.6 |Anaconda, Inc.| (default, Aug 26 2018, 16:05:27) [MSC v.1900 64 bit (AMD64)]

Change the order of python 3.5 and python 2.7.

Then you will get result:

C:\ProgramData\Miniconda2\envs\py2\python.exe
2.7.16 |Anaconda, Inc.| (default, Mar 14 2019, 15:42:17) [MSC v.1500 64 bit (AMD64)]