Configuring Python2 and Python3 under Windows

Source: Internet
Author: User

Objective

Python development is now in full swing and has been applied in all fields, but for some historical reasons Python 2 and Python 3 have been used in the development process. Although the official has announced that by 2020, will no longer support python2, but there are still a lot of software and platform in the use of python2 content. Especially on Linux, a lot of software is also based on the development of Python2.

For the purposes of learning and for better operation and development, we also configure Python2 and Python3 on our own computers. Since MacOS and Ubuntu are already python-based, here we only describe the configuration on the Windows platform.

Directory
  • Download python

  • Install Python 2.x

  • Install Python 3.x

  • Configuring Environment variables

  • Verify that the installation is successful

First, download Python

You can download the Python version you need directly from the official website of Python.
https://www.python.org/downloads/

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/py_down.png "alt=" Python " Style= "Border:0px;vertical-align:middle;"/>

After downloading to a certain directory, the author downloaded the following two versions. The process of installing later is also based on these two versions.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/py_version_10.png "alt=" Python version "style=" Border:0px;vertical-align:middle; "/>

Second, install Python 2.X

     double-click  python-2.7.10.amd64.msi   Start installing Python 2.7. At the same time we can see, python2 by default installed in the C drive, and also named Python27, since this is the case, we will unify a path, used to install Python2 and Python3, convenient unified management.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python_2.10.png "alt=" Python2 installation Path "style=" Border:0px;vertical-align:middle; "/>

Click Next, Next, in the installation of the Python environment variable here, we need to note that we do not install automatically, late will manually install environment variables, because we want to python2 and Python version coexistence.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python2.10_path.png "alt=" Python2 environment variable "style=" Border:0px;vertical-align:middle;/>

Click Next to complete the installation directly.

Third, install Python 3.X

Double-click the Python3 installation package, select Custom installation, and do not choose to add environment variables, we'll manually configure later.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python_3.png "alt=" Python Custom Installation "style=" Border:0px;vertical-align:middle; "/>

Click Next to continue with the installation, to the advanced option here, do not choose, add to the environment variable this option, the reason is consistent with the previous, we will manually configure the environment variables.
Also pay attention to the choice of installation path, should be the same as before, the process of installing python2 is consistent.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python_3_1.png "alt=" Python3 Options "style=" Border:0px;vertical-align:middle;/>

Click Install until the installation is complete. Finally encountered "Disable path length limit", for the moment do not have to control it, basically no impact.

Iv. adding the installed path to the environment variable

Configure the two Python paths that we installed earlier into the environment variables.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python_env_path.png "alt=" Python's environment variable "style=" Border:0px;vertical-align:middle; "/>

V. About PIP

    pip is a Python package management tool that allows you to quickly install and find Python packages. We should know from the information on the Internet, sometimes need to manually install PIP (╮(╯▽╰)╭ or Linux good, not so troublesome). But now python, from the version of 2.7.9+ and 3.4.+, has been installed by default for us. You can also see them from the previous installation options.  
The official also made a description of the link address and, as follows. &NBSP
 https://pip.pypa.io/en/stable/installing/#upgrading-pip

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/python-pip.png "alt=" Python pip "style=" border:0px;vertical-align:middle; "/>

Six, the most critical question, how python2 and Python 3 coexist

A little bit of program development experience of friends know that when we enter a command line, the system will be in the path specified by the environment variables to find the instructions we entered, and our configuration of the environment variables can find two a touch of the same Python instructions, then the system how to know exactly which to use?
When we went to check the information, a lot of articles said that the two paths to the Python to Python2 and Python3, but really only this one way? In fact, the official has already given the answer. can go to the official website to view
https://www.python.org/dev/peps/pep-0397/
Here we refer to an answer from a master and a brief overview.
To view the original answer, click on the link below.
https://www.zhihu.com/question/21653286

How to use Python2 and Python3

When we installed Python3 (>=3.3), the Python installation package actually installed a launcher Py.exe in the system, which was placed by default under Folder C:\Windows\. This initiator allows us to specify whether to run the code using PYTHON2 or Python3 (if you have successfully installed Python2 and Python3).

Suppose we have two separate hello_py3.py written by Python2 and Python3, then we can run it in the following way.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/py-2-3.png "alt=" PY runs the python program "style=" Border:0px;vertical-align:middle; "/>

     each time you enter the-2 or-3 option, it's still a bit cumbersome, and we have another configuration, is to use the Sharp-bang mechanism. Learn about Shell programming friends should understand #! Mechanism. is to add such a piece of code to the first line of code.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/sb_py2.png "alt=" Sharpbang _python2 "style=" border:0px;vertical-align:middle; "/>

The following is the Python3 's wording.

650) this.width=650; "src=" Http://ot2trm1s2.bkt.clouddn.com/Python/2017-10-18-python-1/sb_py3.png "alt=" Sharpbang _python3 "style=" border:0px;vertical-align:middle; "/>

At the same time, in order to ensure the unity of code, we#!The code is followed by a line of code that specifies the encoding, which is also the official recommendation for Python2 and Python3 Common format encoding.
to emphasize, #! The beginning of the code must be written in the first lineAs if the shell were scripting the first line must be#!/bin/bashThe same.
Once the code has been re-modified, it can be used directlypyRun the code in the same way.

Use of the PIP command

Because two sets are installed, the PIP command also installs two sets, in the use of the time will still conflict, we also usepycommand to resolve.
The way to solve it is simple, we just need to precede the command withpy -2Orpy -3This command option will work as expected.
At the same time, because on the Windows platform, you need to add-mOptions.

#python 2py-2-m pip install Xxxxx#python 3py-3-m pip install XXXXX

At this point, our Python installation is ready, then we can write the code happily.


Personal blog Address:http://www.pojun.tech/ Welcome to visit

This article is from the "Fire Captain" blog, please be sure to keep this source http://xiaoshuaigege.blog.51cto.com/6217242/1973830

Configuring Python2 and Python3 under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.