Python Development with Visual Studio Code

Nov. 3, 2017, 5:10 p.m.

Many of my recent projects have been for the web, so I'm much more familiar with tools designed for web development than those for Python. So when I started contributing to DIPY, I hoped to be able to adapt my web development workflow rather than learn another new system. For some time, I've been using Visual Studio Code as my preferred editor. Despite the confusing name, VS Code is not directly related to the full Visual Studio IDE. Instead, it is an open-source, cross-platform editor in the same class as Atom, Brackets, and Sublime. Out of the box, VS Code focuses primarily on JavaScript (and TypeScript) development, but what is the experience like for Python?

Install a Python Extension

After installing VS Code, the first step is to install a Python extension in order to get support for things like autocompletion and linting. I picked Don Jayamanne's extension because it appeared to be the most complete and the most popular. I'll spend the rest of this article detailing how to install and configure this extension.

See the VS Code docs for full description of how to install extensions. But the short version is to click this icon on the left side of the editor and search for 'python'.

Extension icon

Don Jayamanne's Python extension should be the first result, so click on it. You'll see some basic information in the main panel, and you can install by clicking the green Install button.

Update

It seems that Microsoft has now hired this extension's developer and made it the official Python extension for VS Code.

Configure the Extension

Now we need to configure the extension. For full instructions, see the extension's documentation. I'll run through the basics below.

Python Path and Version

First, you need to specify the version of Python you want the extension to use. The easiest way is to open the command palette with Ctrl+p and type 'Select Workspace Interpreter'. This will show you a list of Python versions it finds on your system. If you see the one you want, select it. However, if you, like me, installed Python through Anaconda with all the default settings, you might need to enter it manually. In that case, open the settings file by going to File->Preferences->Settings in the main menu. If you search for 'python', you'll see all the global Python settings in the editor panel on the left. Find the setting called python.pythonPath. If you hover the mouse over that setting, you'll see a pencil icon appear to the left. Click on it to edit that setting in your personal settings file in the editor on the right. Now set that value to the location of your Python executable. In my case, it was:

"python.pythonPath": "c:/programdata/anaconda3/python.exe"

Linting

Next, we'll want to configure a linter. A linter checks your code in the background for errors, including style errors. Since DIPY follows the PEP8 style guidelines, we'll set up the PEP8 linter to nag us whenever we deviate from the style. By default, the extension enables linting with Pylint. Let's change that to PEP8.

First, you'll need to install the PEP8 package with pip. While you're at it, go ahead and install the autopep8 formatter. The extension will automatically use autopep8 without any configuration changes.

pip install pep8

pip install --upgrade autopep8

In the same settings editor from the previous step, locate the python.linting.pylintEnabled setting and set it to false. Then change python.linting.pep8Enabled to true. Your settings should now look like this:

"python.linting.pylintEnabled": false,

"python.linting.pep8Enabled": true

Now VS Code will check your code against the PEP8 standard each time you save a file. You'll see squiggly lines underneath parts of the code that don't conform to the standard.

Debugging

Next, we'll set up the debugger. You can switch to debug mode by clicking the bug icon on the left side of the window.

debug icon

Once you're in debug mode, you'll need to create a debug configuration file, named launch.json. The easiest way is to look for a gear icon at the top of the window with a red circle on it. If you click that gear, VS Code will prompt you to select an environment. Choose Python, then VS Code will generate a new directory called .vscode with launch.json inside. This new file will contain many debug configurations, but you'll likely only use the first one, called Python. You will see those configs in the dropdown menu next to the gear icon, and Python will be selected by default. Now you can create breakpoints in your code and press the green arrow to begin debugging.

A full description of the debugging process is beyond the scope of this article, but for more information, see the VS Code documentation.

Note: don't commit the .vscode directory!

Since your VS Code settings are for you only, make sure you don't commit the configuration files in your git repository. You also shouldn't clutter the DIPY .gitignore file with settings for your specific editor. One alternative is to follow the Explicit repository exludes instructions in the GitHub documentation.

Wrapping Up

That's all you need to start developing in Python with Visual Studio Code. To run your code, just right click anywhere in the code and select Run Python File in Terminal. This will open VS Code's integrated terminal and run your script. To re-run the code, either repeat the right-click procedure or hit the up arrow and enter in the terminal.

The Python extension has several more features like unit testing, code refactoring, and Jupyter notebook support, but a description of those will have to wait for another article. To dig further into the options, see the extension's documentation.


Share: