Python with VS Code

October 11, 2019

Table of Contents

1. Intro

Visual Studio Code (VS Code) is a free, lightweight, cross-platform (meaning it works on Windows, Mac, and Linux) integrated development environment (IDE) created and open-sourced by Microsoft. It provides must-have features such as intellisense and syntax highlighting for writing code and tooling for debugging it, as well as plenty of productivity tools and nice-to-haves (code templates/snippets, color themes, etc.). It can support pretty much any language, and it does this primarily through extensions available on the marketplace (don't worry, despite the name, the vast majority of them are free and open-source as well).

Many of these extensions also provide visual interfaces for working on specific types of projects/workloads. For example, the Microsoft Python extension for Visual Studio Code adds support for writing and running Python code in scripts and in Jupyter Notebooks within VS Code with ease. This quick article will focus on getting you up and running with VS Code and Python to make you as productive as possible.

2. Installation

2.1. Python

In order to run Python code, you need what is called the Python interpreter. This is essentially software that executes the instructions written in the language directly, without having them first be compiled - so Python is an interpreted language, as opposed to a compiled language (such as C#).

Download Python 3.9 (latest at the time of writing): https://www.python.org/downloads/release/python-390/

  • Pick the correct installer for your operating system as noted in the table
  • Run the installer and ensure to tick the box to add to PATH variables if asked
  • To confirm all is well, open up a terminal (e.g. Command Prompt on Windows) and type python and enter and it should tell you the version you just installed

2.2. Visual Studio Code

  • Download VS Code: https://code.visualstudio.com/Download
    • Pick the correct installer for your operating system and architecture (e.g. 64-bit) - for Windows, the User Installer is recommended
    • If asked about adding the VS Code option to the right-click context menus, I recommend going for it! I did not do this originally and wish I had, as it is quite convenient to be able to right click on a folder in File Explorer and quickly open it with another click in VS Code (instead of having to go through VS Code first)

3. Configuration

Now that we have Python installed and we want to make VS Code our editor of choice for it, we need to take some steps to make it an optimal environment. Namely, installing a few VS Code extensions as well as updating some settings to make our lives easier and our coding more productive.

3.1. Python Virtual Environments

Now that you have Python installed, you can start writing code with it. However, what makes computer programming powerful is the fact that you can start using amazing code that has been worked on by others for many years so that you're not constantly recreating the wheel (which often times you/your team don't have the time or even the skills to recreate yourself!). Of course we are talking about Python packages, or libraries, which you can install using a package manager. The default one included with Python is called pip.

Installing packages is simple, you just run pip install PACKAGE_NAME_HERE, e.g. for the famous pandas, you'd execute pip install pandas in a terminal. However, when you do this, it will install the latest version of the package in a global location on your system. And it will get re-used whenever you're writing scripts/notebooks. The trouble is, packages have their own dependencies (packages that they reference) and over time you may run into strange issues as they start to interact with each other in unexpected ways. The solution to that, is creating virtual environments for each major project you do. This doesn't mean you need a virtual environment for every project, but for those that you want to ship or for those that are non-trivial, it is a best practice.

The easiest and recommended way to create and manage your virtual environments is through an additional tool called pipenv. Check out this article for a great introduction to pipenv and more details as to why it's important and how to get started with it. Again, for basic scripts there is no harm in not using it, but at a minimum it's something to be aware of.

Great news is, VS Code makes it easy to work with Python virtual environments, giving you helpful pop-ups and suggestions when it notices that pipenv was used in a project.

3.2. VS Code Extensions

Within VS Code, click on the blocks icon in the left side-panel, or CTRL+SHIFT+X, to open up the Extensions pane. Search for and install the extensions in the list below, ensuring that the name matches exactly. Take a moment to look at the summary for each, which include more detailed descriptions and often-times short videos demonstrating some of their functionalities.

  • Python - this is the main extension to be able to code in Python with VS Code
  • Pylance - this builds upon the standard intellisense provided with the main extension above, particularly in the area of types (giving you more detail about what kinds of objects you're working with in your code)
  • Python Docstring Generator - when you type """ it will bring up a template to help you write your documentation strings faster and in the right format, e.g. for functions that you're writing, which of course lead to better intellisense for you and others reading/using your code.

3.2.1. Bonus Extensions

These aren't crucial by any means, but they certainly make you feel more at home.

  • Dracula Official - an amazing color theme for VS Code, highly recommend (works with pretty much all languages, not just Python). Or check out the many other themes and pick your favorite.
  • vscode-icons - the name says it all - gives your icons a nice upgrade in the Explorer view, as well as provides icons for things that VS Code does not differentiate for at all (e.g. folders with specific names, or filetypes)
  • Trailing Spaces - this will leave little red highlights in your code if you've left a trailing whitespace, although the auto-formatting should pick it up for you anyway when you save

3.3. VS Code Settings for Auto-formatting!

There are a couple of settings we need to tweak to greatly improve our lives and have our code get auto-formatted correctly when we save a file, so we're spending our time writing our code and not making sure every little thing conforms to a desired style. This sounds trivial but actually it not only saves you time, but also ensures consistency across your code that you will come to appreciate, as will others (and for a team it's quite imperative).

  • Within VS Code, go to File -> Preferences -> Settings
  • Search for "format on save" using the search bar and then tick the box for the first setting that comes up
  • Search for "auto format" and ensure there is a setting for "Python > Formatting: Provider". Optional: you may choose to use a different provider, such as "black", but note that you will have to install the black formatter using pip install black for it to work.

4. Conclusion

VS Code really took the dev world by storm - even the likes of Facebook have officially made it their development platform of choice. It was a bit of a surprise coming from Microsoft, which historically had been anti-open-source, but that has completely changed in more recent years, particularly under the leadership of Satya Nadella.

It is my personal favorite, and in particular offers a great debugging experience (you can see all your variables in the script at any given time when you set breakpoints to step through your code) and handles Jupyter Notebooks as well, so you don't need to use a separate interface for that.

However, another great alternative to check out is PyCharm (which has a 100% free communitiy version). At the end of the day, it's all about productivity and getting familiar with our toolset, and between these 2, you really can't go wrong.

Please let me know if you run into any issues or have any questions. Thanks for reading.