Python interpreter in Visual Studio Code

mahesh reddy
6 min readFeb 1, 2021

Using Python 3 in Visual Studio Code to build the easiest Python “Hello World” program. You render VS Code into a great lightweight Python IDE by using the Python extension (which you may find a productive alternative to PyCharm).

This introduces you to VS Code as a Python environment, primarily by using the following tasks to modify, run, and debug code.

  • Post, run, and debug a “Hello World” Python application.
  • Learn how to install packages by building virtual environments for Python.
  • Write a simple Python script for plotting VS Code figures.

Prerequisites

You need to first set up your Python development environment to complete this tutorial successfully. This tutorial, specifically, requires:

  • Code VS
  • Extension of VS Code Python
  • Python 3

Install Visual Studio Code and Extension## for Python

First, install the Python extension from the Visual Studio Marketplace for VS Code. See the Extension Marketplace for more information on downloading extensions. Python interpreter is the name of the Python extension and it is released by Microsoft.

Get a Python interpreter installed

You need to build a Python interpreter alongside the Python extension. The Python interpreter you use is based on your personal needs, but below is some guidance.

With Windows#

From python.org, install Python interpreter. Usually, to download the latest update, you can use the Download Python button that appears first on the tab.

If You are interested To Learn Python You can enroll for free live demo Python online Training

Note:

An additional choice for installing Python on Windows is to use the Microsoft Store if you don’t have admin access. The Microsoft Store offers Python 3.7 and Python 3.8 installations. Be mindful that you might have compatibility problems using this approach with certain packages.

With macOS

Installing Python on macOS is not supported by the framework. Alternatively, an installation via Home brew is suggested. To install Python using Homebrew on macOS, use python3 at the Terminal prompt to install brew.

Note For macOS, make sure that the installation location of your VS Code is included in your PATH environment variable. For more detail, see these setup instructions.

Linux

You have to install pip with get-pip.py to install other Python packages.

Other choices

  • Data Science:

If Data Science is your primary reason for using Python, then a download from Anaconda might be considered. Not just a Python interpreter, but many useful data science libraries and software are provided by Anaconda.

  • Windows Subsystem for Linux:

The Windows Subsystem for Linux (WSL) is a choice for you if you are operating on Windows and want a Linux environment for working with Python. You’ll also want to install the Remote — WSL extension if you choose this option. This is what will lead you through WSL setup, Python installation, and the development of a WSL running Hello World application.

Verify the installation of the Python interpreter

Run one of the following commands (depending on your operating system) to check that you have installed Python successfully on your machine:

Linux/macOS: Open the Terminal Window and type the command below:

Python3 — edition version

Windows: open a prompt for command and execute the following command:

Py-3 — version

If the installation is successful, the Python version you have installed should be displayed in the output window.

Notice

To access versions of python installed on your computer, you can use the py -0 command in the VS Code built-in terminal. An asterisk (*) marks the default Python interpreter.

Start VS Code in folder## of a project (workspace)

Create an empty folder called ‘hello’ using a command prompt or terminal, navigate to it, and open the VS Code in that folder (.) by entering the following commands.

Hi Mkdir Hello

CD Hello

Code

Note:

If you are using a distribution of Anaconda, be sure to use the prompt for the Anaconda button.

The folder becomes your “workspace” by beginning VS Code in a folder. In .vscode/settings.JSON, VS Code stores settings that are unique to that workspace, apart from the user settings that are globally stored.

Alternatively, you can run VS Code via the UI of the operating system, then open the project folder using File > Open Folder.

Choose a Python interpreter

Python is an interpreted language, and you must say VS Code which Python interpreter to use to run Python code and get Python IntelliSense. Select a Python 3 Python interpreter from inside VS Code by opening the Command Palette (Ctrl+Shift+P) and start typing Python: Select the Python interpreter command you want to scan for, then select the command. If available, you may also use the Select Python Environment option on the Status Bar.

You are asking VS Code to view this file as a Python program by using the .py file extension so that it tests the contents with the Python extension and the Python interpreter selected.

Note:

To further organize your code, the File Explorer toolbar also allows you to create directories within your workspace. To build a folder easily, you can use the New Folder button.

Now that you have a code file in your workspace, use hello.py: to enter the following source code.

msg = “Hello World”

Print a Print (msg)

Note how IntelliSense offers auto-completion options when you start printing.

Set up and run the debugger

Let’s try debugging our simple program for Hello World now.

First, by putting the cursor on the print call and pressing F9, set a breakpoint on line 2 of hello.py. Alternately, just click in the left gutter of the editor, next to the numbers of the line. A red circle appears in the gutter when you have set a breakpoint.

Set a breakpoint for hello.py.py

First, click F5 to initialize the debugger. If this is your first time debugging this file, you will be able to pick the type of debug configuration you would like for the opened file by opening a configuration menu from the Command palette.

Note:

It is for all of its different configurations, VS Code uses JSON files; launch.json is the default name for a file that includes debugging configurations.

In Debugging configurations, these various configurations are thoroughly explained; for now, just select Python File, which is the configuration that runs the current file displayed in the editor using the currently selected Python interpreter.

At the first line of the file breakpoint, the debugger will end. The current line is labeled with a yellow arrow on the left side of the line. If you check the Local Variables window at this stage, you will see that the msg variable specified now appears in the Local panel.

The following commands appear from left to right along the top of the debug toolbar: continue (F5), move over (F10), step in (F11), step out (Shift+F11), restart (Ctrl+Shift+F5), and avoid (Shift+F5).

Toolbar Debugging

The Status Bar also changes color to show that you are in debug mode (orange in several themes). In the lower right row, the Python Debug Console also appears automatically to display the commands being executed, along with the performance of the program.

Select the Continue command on the Debug toolbar to continue running the software (F5). In the end, the debugger runs the program.

To run the program to completion, click the blue Continue button on the toolbar again (or press F5). If you turn back to it, “Hello World” appears in the Python Debug Console, and VS Code exits debugging mode once the program is full.

The debugger stops again at the first breakpoint if you restart the debugger.

Use the red square stop button on the debug toolbar (Shift+F5) to stop running a program before it is complete, or use the Run > Stop debugging menu order.

Conclusion

You can customize VS Code to use any installed Python environment, including Virtual and Conda environments. For debugging, you can also use a separate environment. To get more insights, go through the Python online training.

--

--

mahesh reddy

Python certification training course will help you master the concepts and gain in-depth experience on writing Python code and packages like SciPy, Matplotlib,