Python + Visual Studio Code : Microsoft’s VS Code is an easy and convenient code editor available on all platforms and incredibly flexible. This is a great choice for programming in Python.

In this article, we will look at how to install and configure the most efficient development environment.

The article is intended for programmers who already have experience with Python and have installed an interpreter of this programming language on their working machine (Python 2.7, Python 3.6 / 3.7, Anaconda, or another distribution kit).

Installing Python is a simple matter: here you will find a detailed step-by-step guide for all popular operating systems. Remember that the VS Code interface may differ slightly in different operating systems.

Install and configure Visual Studio Code for Python development

Immediately, we note that VS Code has practically nothing to do with its famous namesake Visual Studio.

The editor is very easy to install on any platform: the official website has detailed instructions for Windows, MAC and Linux.


The product is updated and improved monthly. It has integrated multi-language support and a convenient extension model out of the box. The user interface is extremely simple and straightforward.

VS Code + Python

Since 2018, there is an extension for Python . You can watch the development of the relationship of this pair in the microsoft blog.

Main features of the editor:

Python 3.4 and higher support as well as Python 2.7

  • Automatic code completion with Intellisence
  • Lint
  • Debugging
  • Snipples
  • Unit testing
  • Automatic use of conda and virtual enviroments
  • Editing code in Jypyter and jupyter Notebooks

But a couple of useful selections for pumping Python-skills:

  • The Best Books on Python: efficiently, capaciously, intelligibly
  • The Largest selection of Python channels on YouTube

The editor also has useful features that are not directly related to the language:

  • Sets of hot keys for Atom, Sublime Text, Emacs, Vim, PyCharm and many other editors
  • Customizable themes
  • Language packs for multiple languages, including Russian

And a few more cool opportunities for complete happiness:

  1. GitLens – many useful Git features right in the editor, including blame annotations and viewing the repository.
  2. Auto Save ( FileAuto Save ) and convenient setting its delay.
  3. Synchronize editor settings between different devices using GitHub.
  4. Comfortable working with Doker .

To find and install the necessary extensions and themes, click on the Extensions icon in the left pane. You can search by keyword and sort the search results.

Find the Python extension and install it to continue setting up the editor.

Configuration files

In Visual Studio Code, you can easily customize everything for yourself. There are user settings that are global, and the workspace settings are local to specific folders or projects. Local settings are saved as .json files in the .vscode folder.

New Python project

To open a new file, go to the File menu and select New, or press the hot key combination Ctrl+N

Even in the editor there is a useful palette of commands that can be called with the combination Ctrl+Shift+P To create a new file, enter File: New File in the appeared field and press Enter .

Whatever method you choose, you should see a window like this:

Here you can enter the code of your program.

Begin to code

To demonstrate the capabilities of the editor, we write “ Sieve of Eratosthenes ” – a well-known algorithm for finding primes to a certain limit. Let’s start the code:

12sieve = [True] * 101
for i in range(2, 100):

On the screen, it will look something like this:

Wait, something is wrong. For some reason, VS Code does not highlight the keywords of the language, does not complement, does not format, and generally does not do anything useful. Why is he even needed this?

Do not panic! Just now the editor does not know which file he is dealing with. See, he doesn’t have a name or extension yet – just some kind of undefined Untitled-1 . And in the lower right corner is written Plain Text (plain text).

The installation of the Python plugin has already been done, now it needs to be activated. To do this, simply save the file with the desired extension. To do this, we again have three ways:

  • menu: File – Save
  • Hot combination: Ctrl+S
  • command palette: File: Save File

Give the file the name sieve.py .

Now the editor realized that he was dealing with Python code, and corrected himself:

So much better! In the lower right corner appeared Python , then everything works correctly.

If you have several language interpreters installed on your computer (Python 2.7, Python 3.x, or Anaconda), you can choose the one you want. To do this, click on the language indicator (on the bottom left of the screen) or type in the Python: Select Interpreter command palette Python: Select Interpreter .

By default, VS Code supports formatting using pep8 , but you can choose black or yapf if you want.

We add the algorithm code:

123456sieve = [True] * 101
for i in range(2, 100):
if sieve[i]:
print(i)for j in range(i*i, 100, i):
sieve[j] = False

If you enter it manually (without copy-paste), you can see the IntelliSense editor in action.

VS Code automatically indents the for and if , adds closing brackets and offers word completion options.

Run the program

To run the finished program, we do not even need to leave the editor! Just save the file, right-click on the context menu and select the Execute file item in the console .

Now that the code is complete, you can run it. You do not need to exit the editor for this: Visual Studio Code can run this program directly in the Editor. Save the file (using Ctrl+S ), then right-click in the editor window and select Run Python File in Terminal .

At the bottom of the window should appear the terminal panel with the result of the program.

Lint code

You may have already seen the pop-up window with the message that the code check is not available and the offer to install a linter. The default extension suggests PyLint . Other tools are also supported:

  • flake8
  • mypy
  • pydocstyle
  • pep8
  • prospector
  • pyllama
  • bandit

Note that the linter is configured for a specific workspace, not globally.

Editing an existing project

So, we learned how to create new files. This is great, but most of the time you will have to work with existing projects, which consist of many individual files and folders.

You can work with the editor directly from the console, opening and creating files with a simple command code filename.py .

Let’s see what VS Code is capable of using the example of a finished project . This is a library for analyzing equations based on the Dijkstra’s shunting-yard algorithm”. You can clone this repository to get started.

You can open a folder created locally in the editor from the terminal:

12cd /path/to/project
code .

VS Code can work with different environments:Virtualenv, pipeny or conda .

You can also open the folder directly from the editor interface:

  • menu: File – Open folder
  • hotkeys: Ctrl+K , Ctrl+O
  • from the command palette: File: Open Folder

Here is the open project:

By default, when you open the folder, VS Code also opens the files you last worked with. This behavior can be changed.

Now you can open, edit, run and debug all project files listed in the left pane. All files with which you are currently working are displayed above the explorer.

Testing

Competent programming in Python, in addition to actually writing code, also includes testing it.

Visual Studio Code is able to automatically recognize tests in unittest , pytest or Nose . In our project there is a unit test that can be used as an example.

To run existing tests, from any Python file, right-click on the context menu and select Run current test file.

You will need to specify the framework used for testing, the search path and the template for the name of the test files. These settings are saved as workspace settings in a local .vscode / settings.json file. For our project, you need to select unittest , the current folder and the template * _test.py .

Now you can run all the tests by clicking on Run Tests in the status bar or from the command palette.

Also, tests can be performed separately, which saves a lot of time, working only with unsuccessful methods.

Test results are displayed in the Output tab ( Python Test Log section of the drop-down menu).

Debugging code

Despite the fact that VS Code is just a code editor, and not a full IDE, it allows you to debug Python code directly in the workspace. It has many features that a good debugger should have:

  • Automatic variable tracking
  • Expression tracking
  • Breakpoints
  • Call Stack Inspection

All this data can be found in the Debug tab of the left pane

The debugger can control Python applications running in the embedded console or external terminal. It can connect to already running Python instances and even debug Django and Flask applications.

Debugging a Python program is as easy as running a debugger with F5 . Use F10 and F11 to move to the next function and to enter the current function. Shift+F5 – exit from the debugger. Breakpoints are set using the F9 key or by clicking in the left margin of the editor window.

Before you start debugging more complex projects, including Django or Flask applications, you must configure and select a debugging configuration. Make it very easy. On the Debug tab, find the Configuration drop-down menu and click Add Configuration :

VS Code will create and open a .vscode / launch.json file in which you can configure Python configurations , as well as debug applications

You can even perform remote debugging and debugging Jinja and Django templates. Close launch.json and select the desired application configuration from the drop-down list.

Git Integration

VS Code has out-of-the-box native version control support. By default Git and GitHub are connected, but you can install support for other systems. All work takes place in the Source Control tab of the left menu:

If the project has a .git folder, the whole range of Git / Git Hub functions is automatically included. You can:

  • Commit files
  • Update project from remote repository , and send your changes there.
  • Work with existing branches and tags and create new ones.
  • View and resolve merge conflicts
  • Views diffs

All these functions are available directly from the user interface:

VS Code also recognizes changes made outside of the editor.

All modified files are marked with an M marker, and untracked files are marked with U The + symbol prepares files for commit. To save changes, enter a message and press the check mark.

Local commits can be sent to GitHub directly from the editor. Select the Sync item from the menu or click the Synchronize Changes icon in the status bar at the very bottom of the editor (next to the current branch indicator).

Visual Studio Code + Python = Content Developer

Visual Studio Code is one of the coolest code editors and a great development tool. The editor out of the box offers many useful features and flexibly adjusts to all your needs. Python programming is easier and more efficient.

And which editor (or full IDE) do you use for Python development?