debugging python in visual studio , multiple versions of python

mikha used Ask the Experts™
I am new to Python. I have an python project, I need to debug. The program was developed and run with an older version of python (2.7 or earlier) .
I have python 3.7 installed in my windows 7.
I want to install python 2 alongside python 3 and debug the program using python 2.

How can i achieve this?
is virtual env used for this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Just download python2 and install it. Windows and it's python launcher py.exe will then be aware that you installed python2 and python3 on your PC.

from command line ( you can choose the python version by typing.

py -2  

Open in new window

for python2

py -3

Open in new window

for python3

Another way to specify on a per script base which version of python to run is to add following as first line of your python script.

#!/usr/bin/env python2

Open in new window

#!/usr/bin/env python3

Open in new window

virtualenv is a package that allows you to have different virtual environments on your machine.

A virtual environment can only be based on a python version that has been installed on your host.
So if you did not install python2 you can not create a python2 virtual envronment.

Within each environment you can install different python packages, (or different versions of it)

This would allow you for example to test if some code works with django 1.11 (you created one virtualenv in which you installed django 1.11)
and if it works as well with django 2.1 (another virtualenv in which you installed django 2.1)

If you have python2 and python3 installed on your host you can have virtualenvs with either poython2 or python3 as a abase.
Analyst Assistant
When you first open the file in Visual Studio Code you should be asked to select an interpreter if you haven't selected one previously.

If you have set the interpreter previously you can change it by opening the Command Palette (Ctrl+Shift+P), selecting Python :Select Interpreter and then selecting the version you want to use.
I overlooked, that you specifically asked for a solution for visual studio.

My answers assumed you're not using an IDE, that you want to be able to select the python version to be used, when clicking on the python file.
So not really the answer you were looking for.

If you use an IDE, then almost every IDE has somewhere settings to choose the python interpreter on a project basis or as global settings.

Norie gave you the answer for Visual Studio Code, For other IDEs (e.g. PyCharm) there will be somewhere similar settings.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial