2017-05-01T01:23:18Z

Visual Studio Code for Python Developers

In this short article I'm going to give you an overview of Visual Studio Code, a free and open source IDE for Windows, Mac OS X and Linux, from Microsoft. This IDE is highly configurable and extensible with plugins, including a very good one for Python.

Click on this and any of the following screenshots to see a larger image.

Installation

You can head over to the Visual Studio Code home page to download installers for Windows, Mac OS X and Linux. For this article's images and animations I used the Mac OS X version.

Once you have it installed, you would probably want to add the code command to your path, so that you can start an instance from the command line. You can do this by hand according to your operating system, or you can bring up the slick command palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac) and let the program register itself with the system path:

By default, vscode comes with JavaScript support. You can install the python plugin from the command line as follows:

code --install-extension donjayamanne.python

Or if you prefer to do it within the tool, once again you can do so from the command palette with a couple of mouse clicks:

The Python extension requires that you install the exuberant ctags tool, which helps with the symbol extraction from source code files. On the Mac, you can install it with homebrew (brew install ctags), on Ubuntu Linux with apt-get (sudo apt-get install exuberant-ctags). On Windows you will need to download an executable and install it somewhere on your path.

Starting a new project

To start a new project, what I find the most convenient is to create my project in the command line however I like, including a virtual environment with my preferred python version. Then, I can start Visual Studio Code from the root directory of my project with this simple command:

code .

Note the dot, given as an argument, so that a new project is created on the current directory.

There is an extra step that isn't always necessary, which is to select the Python interpreter to use. Normally the interpreter from the virtual environment will be picked up automatically, but I've found that this does not always work. So to be on the safe side, I got used to selecting the interpreter when I open the IDE for the first time on a project. This is done from (you guessed) the command palette:

Configuration

There isn't much to configure, as most sensible options for Python development are enabled by default. Visual Studio Code uses JSON files for configuration. There are two files, the user settings file, for the global configuration that applies to all projects, and the workspace settings file, for the options specific to the project.

Many configuration items can be set interactively, but all the interactive method does is add the setting to the proper JSON file. In the above section I showed you how you can select which Python interpreter to use for a project using the command palette, for example.That translates to the following changes in the workspace settings file:

// Place your settings in this file to overwrite default and user settings.
{
    "python.pythonPath": "${workspaceRoot}/venv/bin/python",
}

Editing the user and workspace config files is a pleasure, because you get popups as you type with help messages.

Intellisense

Speaking of "as-you-type" coding assistance, Microsoft calls that Intellisense. This is also enabled for Python, and works for symbols in the standard library and also for your own code (provided you installed ctags, as I indicated above):

Code Linting

Another useful feature that is also enabled by default is code linting. Whenever you save a file, Visual Studio Code runs pylint and shows a report of all the problems that were found, both as a list, and visually with color squiggles in the code. If pylint is not installed in your virtual environment, Visual Studio Code offers to install it for you.

If you find the output from pylint too detailed (sometimes I do), you can use flake8 instead. You can also enable both if you want. To make these changes, you just edit the JSON config files.

Code Formatting

Visual Studio Code tries to format the code nicely as you type it, but it does not do a perfect job at it. In particular, it is unable to indent multi-line lists of function arguments in the pep8 style (there is an open issue about this on the Python plugin repository). In any case, I've found that it is a lot easier to not worry too much about formatting while typing, and then let autopep8 format your code when you save it.

Note that autopep8 formatting on save is not enabled by default, you need to add this option to the configuration. This is what I added to the config file:

        "python.formatting.formatOnSave": true

If you add this to the user settings file, then all your projects will have an autopep8 pass when you save a Python file.

Debugger

There is also an integrated debugger, with support for line-by-line execution, breakpoints, watches, call stack and popups for variables.

Unit Tests

Visual Studio Code also has a unit test runner that displays these cool overlays above each test that allow you to run or debug that one test. It can also run the entire unit test suite, all the tests in a module, or only the tests that failed in the previous run, if you prefer.

Source Control

I don't really use the source control options much, since I prefer to do all of that from the command line. But if you are more of a visual type, Visual Studio Code allows you to run several git operations directly from the interface. Occasionally I have found the graphical diff view useful, so at least for that I like to have source control in the tool. You can see a screenshot of this view at the top of this article.

Final Words

I hope you found this walkthrough useful. If you liked what you saw, I encourage you to give Visual Studio Code a try. I have been using it regularly for the last couple of months for a variety of projects, both personal and from work, and have found it fairly stable. I think it is not quite as polished as PyCharm, but on the other side, it seems lighter and faster, so overall I'm pretty happy with it.

20 comments

  • #1 Leandro E. Colombo ViƱa said 2017-05-01T02:50:55Z

    Great walkthrough Miguel! Pretty straight forward on several key issues un Python development. I'm a PyCharm user, and I'm very happy with it. Even thought sometimes It's a little big heavy and slow, as you had mentioned... But I'm looking something to include in my teaching. I believe that an IDE for a 101 introduction to programming It's too much Information for students to absorbe. I've been using Atom with my students but I'm not very pleased with UX in Python Development, maybe I'll give it a shot with VS Code. Thanks!

  • #2 Sanjayshr said 2017-05-01T19:51:02Z

    It's been a year now I using Vim it's pretty faster but lags when it comes to plugins, I just tried VS Code its just awesome :), Waiting for Part 2 of "Flask Mega-Tutorial". Thanks!

  • #3 Sts said 2017-05-01T22:44:05Z

    Does it have support for more data science workflows with lots of plotting and notebooks etc?

  • #4 Miguel Grinberg said 2017-05-02T02:26:50Z

    @Sts: Not that I know of.

  • #5 Pouya said 2017-05-05T00:36:10Z

    It's worth mentioning that if you are after a full-fledged IDE (similar to pycharm) Microsoft has PTVS (Python Tools for Visual Studio) to offer.

  • #6 Tony said 2017-05-05T02:59:47Z

    @Sts -- The VSCode python plugin formerly had support for Jupyter notebooks (including plot visualization), but the author moved it to a separate plugin. https://marketplace.visualstudio.com/items?itemName=donjayamanne.jupyter

  • #7 Miguel Grinberg said 2017-05-05T05:59:41Z

    @Pouya: I don't disagree that PTVS is a good IDE, but Visual Studio Code is a full-fledged IDE as well. Also PTVS runs only on Windows.

  • #8 Simon Wolf said 2017-05-11T11:17:27Z

    Regarding the Linting section, if you want some more information about enabling and disabling linters then I found this wiki page useful: https://github.com/DonJayamanne/pythonVSCode/wiki/Linting

  • #9 erhuabushuo said 2017-05-12T03:47:57Z

    Hi, Miguel. is there any way to generate docsting automatically.

  • #10 Miguel Grinberg said 2017-05-12T05:07:08Z

    @erhuabushuo: I don't think so. But you can create your own module or function templates exactly as you like them and store them as user snippets that you can insert when you need a new element.

  • #11 Allyn H said 2017-05-16T15:14:30Z

    Hi Miguel / all, I've just downloaded VS code as per this blog post and I'm finding it great. Are tehre any other plugins that you'd recommend for either web dev or Python?

  • #12 Kuldeep said 2017-07-21T21:10:50Z

    I am new to both Python and VS Code and I struggling to find a way to add Python modules to VS code. Can you pls show the steps/cmds to add any Python modules to VSCode project.

  • #13 Miguel Grinberg said 2017-07-22T00:20:46Z

    @Kuldeep: There is nothing special about adding Python modules to a project. As long as the project you open has Python files, they should be shown by vscode. Make sure you install the Python plugin to have a much better experience with these Python source files, though.

  • #14 T said 2017-08-15T23:27:24Z

    My favourite editor! Works great with Raspberry Pi when using cyberduck + SFTP to edit the python code.

  • #15 Dan Griscom said 2017-10-11T20:13:44Z

    Your Exuberant Ctags link is misspelled (no "h"), and broken (you have to use "http" rather than "https"). The correct link is http://ctags.sourceforge.net/ .

  • #16 Miguel Grinberg said 2017-10-11T21:39:04Z

    @Dan: Thanks, both corrected!

  • #17 Yuri said 2017-10-15T16:36:17Z

    Miguel have you ever tried something that works for jinja2?

  • #18 Miguel Grinberg said 2017-10-15T16:59:33Z

    @Yuri: What do you mean? You can edit Jinja2 templates with code just fine, I do it all the time.

  • #19 Tristan Trouwen said 2017-11-05T14:37:05Z

    Since the python extension uses ptvsd instead of pydevd for debugging it is extremely slow for some larger projects. A simple flask server can take up to two minutes to load. Have you got an alternative debugging solution?

  • #20 Miguel Grinberg said 2017-11-05T14:40:31Z

    @Tristan: The vscode debugger hasn't been slow for me at all. Two minutes? I have it up and read in a couple of seconds.

Leave a Comment