A Comprehensive Guide to Getting started with Python in Visual Studio Code9 min read

This article originally appeared on Kite

Table of Contents

Introduction to VS Code

Visual Studio Code (VS Code) is a source code editor developed by Microsoft that can run on Windows, macOS, and Linux. It is free, open-source, and is embedded with debugging tools, integrated terminals, built-in Git version control, code navigation, refactoring, and so on.

In addition to the built-in features, VS Code is highly customizable as users can install extensions to add additional support for languages, themes, and debuggers – among others.

Although VS Code comes with built-in support for JavaScript, TypeScript, and Node.js, it has a vibrant ecosystem of extensions for other languages including Python.

In this article, we’ll explore the vibrant support VS Code provides to make editing source code simpler for Python developers. We will walk through installing, browsing and adding extensions, as well as other useful packages to help with the Python development.

Initial Installation

Installing VS Code on MacOS

In this example, we’ll install the VS Code on macOS. Remember to install a Python distribution.

  1. Download VS Code
    Open Chrome or your browser of choice, and head to the VS Code download section, and find the appropriate install for your platform (Windows, macOS, and Linux).
  2. Begin Installation
    Once the VS Code installation files have been downloaded, run the program and follow the instructions. Drag the program into applications, and verify the installation as requested.
  3. Add to the Dock
    If you want to add VS Code to the dock, right-click on the icon and select the option to keep it in the doc.
  4. Launch from the command line
    VS Code makes it super convenient for the developers to open the editor through the command line.
    • In your editor, click COMMAND + SHIFT + P to open the command palette
    • Type Shell and a list will appear
    • Select the one that says: Shell Command: Install code in PATH

Open your terminal, go to your preferred directory and type code. (Notice the space between the word code and the period). It will open the folder in VS Code.

Installing VS Code on Windows

Installing on Windows is simply a matter of downloading the executable installer and running it as you would most other Windows applications.

  1. Download the Visual Studio Code installer for Windows.
  2. Once it is downloaded, run the installer (VSCodeUserSetup-{version}.exe). This will only take a minute.
  3. By default, VS Code is installed under
    C:\users\{username}\AppData\Local\Programs\Microsoft VS Code

Installing VS Code on Linux

Depending on your Flavor and preferences, there are a few different ways to install VS Code on Linux. Here are the three most popular:

  • Package Manager (Beginners)
  • Snap (Official)
  • Apt (Ubuntu)

For Fedora, Redhat, and CentOS, check out the terminal instructions here.

Desktop Package Manager (Easiest for Beginners)

  1. Download the appropriate .deb or .rpm file from https://code.visualstudio.com/download
  2. Right-click and select “Install with Package Manager” depending on your Desktop Environment.

Snap (Official)

Snap is the “official” way to install VS Code from the terminal according to Microsoft.

Simply run:

$ sudo snap install --classic code

Note that you will need to have Snap installed on your system for this to work. You can also use code-insiders in the command for VS Code Insider installation version.

Apt (Ubuntu)

Ubuntu users may wish to add the repository and install it with apt.

1. Add the Repository

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$ sudo install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

2. Update and install

$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install code # or code-insiders

The Python extension (all versions)

Before you can use Python with VS code, you’ll need to install the Python extension. The extension is published by Microsoft and can be found in the extension store by:

  1. Starting VS Code,
  2. Selecting Extensions in the menu panel on the far left,
  3. Searching for the Python extension from Microsoft and hitting ‘Install’.

Python interpreter selection

Now that we have VS Code installed with the Python plugin, let’s set our Python interpreter. Python is an interpreted language which means VS Code needs to know which interpreter to use to run our Python scripts.

In this article, we’ll be following steps for Python 3 so make sure you have Python 3 installed. If you want to continue with Python 2, the steps are relatively similar.

  1. In your editor, open the command palette by using the keyboard shortcut  COMMAND + SHIFT + P   
  2. Search for Python: Select Interpreter
  3. Then select your Interpreter from the list

VS Code Python features

Python debugging:

One of the key features of VS Code is its debugging support. Automatic variable tracking, watch expressions, breakpoints, and call stack inspection are some of the debugging features VS Code provides.

Debugging your current Python file is as simple as pressing F5. As soon as the debugging session starts, a few things happen:

  • The debug console panel appears with the commands that are being run and the output
  • The debug view to the left of the editor with all the information related to debugging
  • The debug toolbar appears along the top with debugging commands and configuration settings

The debug toolbar has the following commands from left to right: continue (F5), step over (F10), step into (F11), step out (⇧F11), restart (⇧⌘F5), and stop (⇧F5).

We can also toggle breakpoints by clicking on the editor margin or using F9 on the current line. When you set a breakpoint, a red circle appears in the gutter.

For more breakpoint control, we can edit the respective section in the Debug view.

For more complex projects, it’s helpful to configure and save debugging setup details. We can achieve this by creating a launch configuration file.

Creating a configuration file – launch.json – is pretty straightforward. From your project folder in VS Code, select the Configure gear icon on the Debug view top bar, and select Add Configuration to add Python.

Version control

VS Code has built-in support for source control management and includes Git support in-the-box. Additional SCM providers can be installed through extensions on the VS Code Marketplace.

The source control tab can be enabled by clicking the third icon in the activity bar or ⌃⇧G. It shows the details of all the changes, staged changes and merge changes. Apart from displaying the repository changes, VS Code also offers you to commit files, clone repositories, push and pull changes, create and checkout branches, view and resolve merge conflicts, and view diffs.

Formatting

For programmers, readability is crucial, and code formatters make sure to improve the readability of your Python code. VS Code’s Python extension has default support for autopep8.

Autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style guide. PEP 8 has specific guidelines for line spacing, indents, spacing around operators, and so on. By following these conventions, autopep8 makes the code easier to comprehend.

To format a file use ⌃⇧F or simply run the following command:

$ autopep8 --in-place --aggressive --aggressive  <filename>

Where  --in-place makes changes to files in place and --aggressive enables non-whitespace changes.

Run selection

There are many ways of running your script. But if you want to run a selected code or the code on the current line if there is no selection, SHIFT + ENTER is a way to do it. It will run your code in the Python Terminal.

Alternatively, you can select Python: Run Selection/Line in Python Terminal from the command palette to run the selected code.

Linting

The Python extension also comes with linting. As soon as you save the file, linting helps with analyzing the script for common syntactical, stylistic, and functional errors. It displays a list of all the issues found and depicts them visually with color squiggles in the code.

VS Code comes with the default linter – PyLint – but it can easily be customized in the command palette by selecting Python: Select Linter.

Popular extensions:

VS Code is an excellent editor on its own, but you can increase its power through Extensions. Extensions let you customize and support your development workflow.

You can browse and install extensions from within VS Code. To bring up the Extensions view, click on the Extensions icon in the Activity Bar or use ⇧⌘X. It will display a list of recommended as well as enabled extensions.

Click on the Install button and after a successful install, you’ll need to reload to enable the new extension.

Python

This is the official extension by Microsoft you installed earlier. It has rich support for all versions of Python. It includes features such as automatic indenting, linting, debugging, code navigation, code formatting, code definition, refactoring, unit tests, snippets, Python Interactive (Jupyter support), among others.

Kite

Kite is an AI-powered programming assistant that helps you write Python code inside Visual Studio Code. The Kite Engine needs to be installed in order for the extension to work properly. The extension itself provides the frontend that interfaces with the Kite Engine, which performs all the code analysis and machine learning.

Kite provides you with:

  • Smart autocompletion powered by machine learning models trained on the entire open-source code universe
  • Function signatures that show you the official signature of a function you’re currently using
  • Instant documentation for the symbol underneath your cursor

autoDocstring

With more than eighty thousand downloads, autoDocstring by Nils Werner quickly generates docstrings for Python functions, chooses between several different types of docstring formats, and supports for args, kwargs, decorators, errors, and parameter types among other features.

Other popular plugins include Docker integration, Code Runner, Anaconda Extension Pack Trailing Spaces, and GitLens.

Getting the most from VS Code

VS Code is a powerful code editor and can be enhanced with the installation of a few extensions. If you want to get the best out of the VS Code, learn the ins and outs of the editor by going through the Interactive Playground. It provides a step-by-step guide to some of the advanced code editing features with interactive examples.

Facebook Comments

Previous Article
Next Article

Leave a Reply

avatar
  Subscribe  
Notify of

Sign up for newsletter

* indicates required

Categories

Archives