How do I switch between Python 2 and 3 under the same computer?

Source: Internet
Author: User
Tags virtualenv
The teacher is using Python 2, but I think I now learn to learn a new bit better, 2 and 3 said the difference is quite big, so two versions are installed, their own learning when using 3, run the program with 2.
My Computer is Win 7, now the system is called by default is 2, if the execution before the "C:\python33\python.exe" can call 3, but this is too much trouble, I add "C:\python33" in the path of the environment variable has no effect, Do not know how should also be set up can be convenient to switch?

Reply content:

Well, I'll introduce an artifact to the title, Virtualenv, to create a number of isolated Python workspace, not only to specify the Python version, but even packages can be different.
A couple of open source Python projects are recommended by others.
How to use Python virtualenv
Use different Python version with virtualenv Python on Windows has its own launcher Py.exe, which is installed by default in the System32 folder of the system disk. If you have both Python 2 and Python 3 installed, enter it directly in the terminal:
py -3
1.
Virtualenv
This is basically the Python development standard, then you can control a Python environment in a directory

2.
Pyscripter also has the ability to switch Python versions, you can rename the Python.exe file in Python3 to Python3.exe
In this case, the execution commands of 2 and 3 can be separated, and you need to switch. Why are environment variables automatically written when I install on Windows? There is a very general solution, the principle is detailed in 3. Using Python on Windows
python3.3 or later copies both the Py.exe and Pyw.exe runtimes to the system directory%systemroot% at installation time.

If you normally run Python from the command line, then whenever you do, enter
py filename.py或者py -3 filename.py
Python 3.3 Provides a new launcher to address version switching issues on Windows.

Official Description:
The
Python launcher for Windows are 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 would locate and execut e that version. Reference:3. Using Python on Windows

Simply put, on the basis of the installation of Python 3.3, the py is guaranteed to be in the environment variable. To invoke Python from the command line:
py -2.7py -3
Two, like a path.
D:\python27
D:\python35

Then go to the python27 and change the python.exe underneath the Python2.exe
Python35 the python.exe below, change to Python3.exe.

And the first line of your foot is like
#! D:\python27\python2.exe
This line of work is to adjust the Python2
To adjust the Python3 I provide a way to first install a software Enthough Canopy, which is a release with a variety of scientific computing Python libraries, such as panda,numpy and a variety of drawing libraries are built-in convenience.

Canopy also integrates the online upgrade and management system of the Pyhton package, which is very convenient. Due to its commercial level, canopy has a good performance and stability. Free versions and academic editions (for educational research are also free) are also available.

Formerly called EPD, now changed to name 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. But it is primarily about performance and stability, so it is not expected that all the installation packages are up to date.

If you have a school mailbox, you can free commercial version of a year, if not also, free library is also very useful, and the question itself asked how to install the 2.7 and 3.5 at the same time, so the free version is enough. The canopy is based on 2.7, with 32-bit and 64-bit options. After the installation, there will be a DOS command line running icon,
At this point, you can then install the python3.5 version, from the General Command line DOS or PowerShell access is 3.5. The two do not affect each other, the library resources are also independent. This should be a better solution.
If the teacher asks Python2 to refer to Python2.7, you can write Python2 and Python3 almost completely compatible code.

The trick is to write "forward compatible" code. Writing forwards Compatible Python Code

The actual application process is not that you write a code yourself two times, but instead use unit test to run automatically in multiple Python interpreter. I looked at it. Pycharm is a support for configuring multiple copies of test, each of which corresponds to a interpreter. It's not clear. How to Get Started with Pycharm and has a productive Python IDE Here's the UnitTest chapter.
  • 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.