How do I switch between Python2 and 3 on the same computer?

Source: Internet
Author: User
Tags virtualenv
The teacher uses Python2, but I think it is better to learn something new. The difference between 2 and 3 is said to be quite big, so both versions are installed, use 3 when you are learning, and use 2 when running the teacher's program. My computer is Win7, and now the system calls 2 by default. If "C: python33python.exe" is added before execution, you can call 3, but this is too troublesome, adding "C: python33" to the path of the environment variable does not work either. I don't know how to set up the teacher to use Python 2, however, I think it is better to learn new things. The difference between 2 and 3 is said to be quite big, so I installed both versions and used 3 when I learned them, 2 is used to run the teacher's program.
My computer is Win 7, and now the system calls 2 by default. If "C: \ python33 \ python.exe" is added before execution, you can call 3, but this is too troublesome, adding "C: \ python33" to the path of the environment variable does not work. I don't know how to set it for easy switchover? Reply content: Well, I will introduce the subject to an artifact, virtualenv, which can create several mutually isolated python workspaces. Not only can the python version be specified, but packages can be different.
I have used several open-source python projects, which are recommended by others.
How to use Python virtualenv
Use different Python version with virtualenv Python on Windows comes with the starter py.exe, which is installed in the system32 folder of the system disk by default. If you have installed Python 2 and Python 3 at the same time, enter:

py -3
This is basically the standard for python development. Then you can use a python environment in a directory.

Pyscripteralso has the function of switching you can change the name of python.exefile in python3to python3.exe.
In this way, the execution commands 2 and 3 can be separated, so that you can switch between them. Why are environment variables automatically written when I install them on Windows? There is a common solution. For details, see 3. Using Python on Windows. .
Python3.3 or a later version copies the py.exe and pyw.exe runners to the system directory % SYSTEMROOT % during installation.

If you run python through the command line, enter

Py filename. py or py-3 filename. py
Python 3.3 provides a new Launcher to solve the version switching problem on Windows.

Official description:
The Python launcher for Windows is a utility which aids in the location and execution of different Python versions. it allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version. reference: 3. using Python on Windows

To put it simply, on the basis of installing Python 3.3, ensure that py calls python from the command line in the environment variables:

py -2.7py -3
For example
D: \ python27
D: \ python35

Then, change python.exenext to python27to python2.exe.
Change python.exeunder python35to python3.exe

Then, the first line of your notebook will add a similar
#! D: \ python27 \ python2.exe
Such a row uses python2.
To use python3, Let me provide a way to first install the software Enthough Canopy, which is a release version with a variety of scientific computing python libraries, such as panda, numpy and various image libraries are built in for convenience.

Canopy also integrates the online upgrade and Management System of the Pyhton package, which is very convenient. Because it is commercial, Canopy has good performance and stability. Free and academic versions are also available (for educational and scientific research purposes ).

Previously called EPD, And now renamed it Canopy. Canopy is the first release to upgrade Ipython to 2.3.0. MatPlotLib has been upgraded to 1.4.2; NumPy 1.8.1; Scipy 1.2.0. however, it mainly pursues performance and stability, so it cannot be expected that all installation packages are up-to-date.

If you have a school email address, you can use the commercial version for free for one year. If you don't have it, the free database is also very useful. The question itself asks how to install the free database on both 2.7 and 3.5, so the free version is enough. Canopy is based on 2.7 and has 32-bit and 64-bit options. After installation, a running icon of the doscommand line will be displayed,
In this case, you can install Python 3.5 on your own. From the common command line dos or powershell, it is. Both of them do not affect each other, and the database resources are also independent. This should be a better solution.
If the instructor requires Python2 to be Python2.7, you can write code that is almost fully compatible with Python2 and Python3.

The trick is to write forward-compatible code. Writing Forwards Compatible Python Code

In actual application, you do not write a piece of code and run it twice, but use Unit test to run it automatically in multiple Python interpreter. I have seen that PyCharm supports configuring multiple test copies. Each test corresponds to an interpreter. I don't know How to Get Started with PyCharm and Have a Productive Python IDE Unittest section.

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: 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.