Have you heard about major two versions of Python?
There are two major Python versions – python 2 and Python 3. They have different compatibility.
The program running on one Python version not necessarily work with another Python version.
So, many times we have to two deal with both the Python versions for running different Python projects on the same system.
Let’s see how to Install and Run different Python versions in CMD prompt.
Installation of Python is pretty easy as like any other software you install on your system. You can simply download Python software from its official website and install it.
For more detail, choose below installation steps based on the operating system.
Follow the same steps for installing Python 2 and Python 3 versions.
Now you have two Python version installed on your system. But, how to choose a particular Python version to run your Python code or script.
Simply add the following line of code as the first line to your any Python script.
You can print the Python version to ensure it.
#!python2 import platform print(platform.python_version())
Save it as
pyVersion.py (or any file name you suggest). And run the following the command
>> py pyVersion.py 2.7.13
Add this line to your Python script.
Run the same program again by changing the first line with the above line of code.
#!python3 import platform print(platform.python_version())
>> py pyVersion.py 3.7.0
Here, the first line of the code is called as Shebang Lines. And it starts with
The main purpose of the Shebang line is to indicate how a script should be executed on the system.
If you are Linux or Unix user, these operating systems have native support for executing Shebang lines. Same shebang lines, you can use with Python script on windows as well.
This simple one line of code to run different python versions in cmd on the same system helps me a lot for many of the Python projects which has a dependency for running on particular Python version.
As this line is part of Python script, it works for any operating system like WIndow, Linux, and iOS.
I do write Python code every day and share my knowledge. Check out my complete Python tutorial for more of such tricks.