Flexible switching of multiple versions of Python in use

Source: Internet
Author: User
Tags windows 7 x64

Today, let's talk about how to gracefully switch between multiple versions of Python on a Windows system.

Although Python3 has been out for a long time, although Python2 is about to become a history, but for historical reasons , there are still many company's old projects continue to use the Python2 version (switching costs too high), so most developers on the machine Python2 and Python3 are both co-existent, this article mainly explains how to conveniently switch between Python2 and Python3 in this case.

First of all, this time we do not introduce virtualenv, nor introduce pipenv, because these two are for large-scale Python project preparation, and then separate text to explain.

This time is not the use of external tools, to achieve fast switching. In fact, it can be said that the use of system environment variable logic to achieve the purpose.

This environment takes Windows as an example:

System: Windows 7 x64
Python version: Python 3.4 and Python 2.7

Installation

First install the Python3.4 and Python2.7, the installation package can be downloaded in this place: click to jump.

Setting environment variables

Then add the corresponding version of the installation path to the system environment variables:

A new CMD window verifies the effect after adding:

C:\Users>pathPATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Python34;C:\Python27;

At this time we directly in the cmd input python, can already be recognized, but always identify the path in the environment variables in the previous version of the python, such as the current from see Python3.4 is the front of the row, the actual verification under the look:

C:\Users>pythonPython 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>
Renaming the main program

We then changed the two versions of the Python main program EXE to the next name, 3.4 version of the renaming of the python34.exe,2.7 version of the renamed to Python27.exe:

Then run Python34 and python27 separately:

C:\Users>python34Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>C:\Users>python27Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>

As you can see, we can already implement the effect of running the specified version separately, if the script is executed directly python34 test.py or simply python27 test.py .

But each execution of the script prefix is not very tired, if you have a version is commonly used, you can also modify the File Suffix Association program to implement the default association, and then need a special version of the time to add a prefix to the line, but for the sake of insurance, or always carry it.

Additional Information

Supplemental instructions, in fact, there are online users have provided the other two ways:

Use Python's own py -2 and py -3 commands;
The other is similar to what I said above, but renamed only one version of the execution file name;

If the machine has only two versions of Python installed, these methods can all be solved, but because I need some scripts to support Windows XP (Python3.5 will not support XP later), Python2.7, Python3.4, and Python3 are installed. .6 A total of three versions of Python, so that all the main program files can only be renamed, and this solves the problem more thoroughly, once and for all.

Again, if you want to call PIP for different versions of Python, the command is as follows (please replace requests with the package name you need):

python27 -m pip install requestspython34 -m pip install requestspython36 -m pip install requests

The dependent libraries that are installed are independent of each version.

All right, let's try it. Of course, if you have better suggestions, please leave a message to discuss.

This article sylan215
This address: http://www.sylan215.com/
Copyright Notice: This article starting in the public number "sylan215", can be reproduced at will, but must be in a clear location to indicate the source!

Flexible switching of multiple versions of Python in use

Related Article

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.