Home > Backend Development > Python Tutorial > Detailed explanation of how to coexist between Python2 and Python3 versions under Windows

Detailed explanation of how to coexist between Python2 and Python3 versions under Windows

高洛峰
Release: 2017-02-13 13:36:25
Original
1553 people have browsed it

Preface

I have always used Python 3. Recently, I have been studying WeChat public account development. Each cloud platform only supports Python 2.7. If you want to use other versions, you need to set up your own environment. There is no information on developing WeChat public accounts with Python 3 on the Internet. I plan to use Python 2.7 first and then migrate to Python 3 when I have time to learn Docker.

After installing Python 2.7, scripts that originally worked normally under 3.4 cannot run. Some methods on the Internet are to rename the two versions of the main program to python2 and python3 respectively, judge the script with the human eye, and execute the script by manually entering the command line. Of course, lazy people like me who like to double-click and drag will not be satisfied and have found a smarter solution.

Python 2 and Python 3 coexist

When installing Python 3.3 or above, Python will install the launcher py.exe in the C:\Windows folder.

Double-click the script to call this program:

Detailed explanation of how to coexist between Python2 and Python3 versions under Windows

If both Python 2 and Python 3 exist in the system, you can use it to specify the version to run the code :

py -2 helloworld.py
py -3 helloworld.py
Copy after login

2 and 3 are the versions.

It is too troublesome to add parameters every time. Specify the version directly on the first line of the Python script:

#! python3
Copy after login

You can double-click or command line Run:

py helloworld.py
Copy after login

If you do not specify the version on the first line and run or double-click the above command, it will call Python 2 by default

Use pip:

py -2 -m pip install requests
py -3 -m pip install requests
Copy after login

-m pip means running the pip module

The original python and pip commands are still valid. Which version is executed by default? Look at the order of paths in the environment variables.

C:\Python34\;C:\Python34\Scripts;C:\Python27\;C:\Python27\Scripts;
Copy after login

As above, call Python 3

For more detailed explanations on the coexistence of Python2 and Python3 versions under Windows, please pay attention to PHP Chinese for related articles. net!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template