Setup Visual Studio Code for Python Development

In this tutorial, we will have an overview of an open source IDE VS Code and why we choose to go with it for Python development. We will also see how to setup Visual Studio Code for Python development and configure it for a sample Python based Flask hello world project.

Setup Visual Studio Code for Python Development - TutLinks
Setup Visual Studio Code for Python Development – TutLinks

Table of Contents

What you will learn

By the end of this tutorial, you will have an understanding on

  • Brief introduction and overview of Visual Studio Code.
  • Why we are choosing VS Code for Python application development.
  • Installation of VS Code Plugins that help ease Python development.
  • Configuring the VS Code Plugins for a simple Flask project.

Overview of Visual Studio Code IDE

Visual Studio Code is an Integrated Development Environment, simply an editor to code, write and debug software applications written in various programming languages. Visual Studio Code is an open-source offering from the Microsoft. VS Code has proven to be the best among any other IDEs in the world of Software Development due to large community, support, vast number of plugins than any other IDE and huge number of downloads. VS Code offers various capabilities with the help of plugins and has been a go to IDE for many software developers.

Why I prefer VS Code for Python Application development

I’m not biased and do not have any affinity towards VS Code. But over the period of time I found myself comfortable using VS Code for developing not just Python Project, but it has also become a go to IDE for C#, JavaScript, PHP and Go language development.

With VS Code, you can build or write software for any programming language and the IDE does not limit itself to any set of software programming language. Be it a Go, Python, C# or Ruby, VS Code allows to develop software with the integrations and plugins available. It has rich set of version control plugins, plugins that help you connect remote servers, powerful debug capabilities to mention a few.

You can customize the editor with preferences and settings or have a different look and feel by choosing various color themes (Ctrl+K Ctrl+T) it offers. VS Code comes with minimal UI and Menus with high control over the GUI aspects of IDE via rich set of customizable keyboard short cuts which most of the developers love. Almost all programming languages has intellisense support offered via respective plugins.

The other popular alternatives to VS Code IDE for Python development are PyCharm, Visual Studio, Atom to mention a few. I will be using Visual Studio Code and will be setting up the IDE for the Python Programming Development.

Configure VS Code IDE for Python Development

We will first install the basic VS Code plugins to help ease our Python development. Later we will configure the installed plugins for the sample Python based Flask project that we want to work.

Install Python Extensions / Plugins for VS Code IDE

Install the following extensions to help you write Python programming on VS Code IDE.

  • ms-python.python extension for Python development that has the capabilities like Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more.
  • eamodio.gitlens, GitLens — Git supercharged extension for Source control management via Git. Here is the description from the official GitLens plugin about what it offers. GitLens supercharges the Git capabilities built into Visual Studio Code. It helps you to visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more.
  • donjayamanne.githistory Git History to View git log, file history, compare branches or commits.

You are free to install other extensions you find comfortable working with at any point of time as you explore.

Configure VS Code Plugins for Python

After these extensions are installed, configure the VS Code IDE for Python development.

Please be noted that the following settings need to be done when you are creating a new Python project or on an existing Python project. You also can create a basic Hello World Flask based Python app and try configure Python plugins for VS Code IDE.

You need to create Virtual Environment before you set things up for the VS Code IDE. For that you can refer this detailed tutorial on Working with Virtual Environments in Python for Beginners. Otherwise you can also refer to this video that walks you through the detailed tutorial on How to manage Python projects using Virtualenv

Configure Python Interpreter in VS Code

In the VS Code IDE, hit the keys Ctrl+Shift+P and Type Python:Select Interpreter in the text box prompt that pops up at the top middle of the VS Code IDE to choose Interpreter. Choose the path that points to your virtual environment.

Configure Linter

A Linter is generally a program that does the static code analysis. Linter will help reduce compile time errors that arise due to syntax errors. Every program has its own static code analyzer aka linter and can be installed as an extension if available in VS Code.

In VS Code hit the keys Ctrl+Shift+P and Type Python:Select Linter in the text box prompt that pops up at the top middle of the VS Code IDE to choose linter. Up on hitting enter, choose linter of your choice. Let’s choose pylint.

A file named settings.json will be created with references to Interpreter and Linter. In settings.json file located under .vscode directory at the root of the project.

Ensure you point the python.pythonPath to the virtual environment’s python executable. Also ensure the linter preferences are enabled. On Windows OS, the .vscode\settings.json should look similar as below.

{
    "python.pythonPath": "{your-env-name}\\Scripts\\python.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true
}

Check in the .vscode directory to source control

It is a good practice to check the .vscode directory to the source control. Doing so will enforce and ensure all the contributors of this repository will have same settings specific to a project.

Video Tutorial

Navule Pavan Kumar Rao

I am a Full Stack Software Engineer with the Product Development experience in Banking, Finance, Corporate Tax and Automobile domains. I use SOLID Programming Principles and Design Patterns and Architect Software Solutions that scale using C#, .NET, Python, PHP and TDD. I am an expert in deployment of the Software Applications to Cloud Platforms such as Azure, GCP and non cloud On-Premise Infrastructures using shell scripts that become a part of CI/CD. I pursued Executive M.Tech in Data Science from IIT, Hyderabad (Indian Institute of Technology, Hyderabad) and hold B.Tech in Electonics and Communications Engineering from Vaagdevi Institute of Technology & Science.

This Post Has 2 Comments

Leave a Reply