Python Dev environment with Visual Studio Code on Linux

A part of You may be familiar with Visual Studio Code and some of You is probably seeing it for the first time. I'm using VS Code on Linux (Mint 17.3) for Python development. I'm also using PyCharm from JetBrains - my choice depends on what I need at the moment: a big powerfull IDE or quick and powerfull editor.

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

For me, the main feature of VS Code is the extensibility. When installed, You get a solid base with few programming languages support and git support. But there is also an extensions system, connected to Visual Studio Marketplace. You can both use extensions from the Marketplace and publish Your own extensions.

Installation

OK, the first thing is to install VS Code (on Linux). Just go to https://code.visualstudio.com - Your OS will be detected and if You are visiting the page from Linux, the packages for Linux will be proposed. You can get .deb or .rpm packages or ZIP archive - all in both 32bit and 64bit architecture.
All You need to do is to download package and install it with packages manager (i.e. dpkg on Mint / Ubuntu / Debian). I'm using GDebi (default .deb installer on Mint).
You should install VS Code >= 1.3 or upgrade to >=1.3 if You have an older version. There was a lot of bugfixes and new features in 1.3.

Python support

Enabling good Python support in VS Code is very simple. All You need to do is to install a Python extension. There is few Python extensions in the Marketplace, but one that is much more popular, has great development flow. It is also suggested by Microsoft as a proper Python extension for VS Code: https://marketplace.visualstudio.com/items?itemName=donjayamanne.python
If You have VS Code in version >= 1.3, press the Ctrl + Shift + X key combination - You will get to the extension module. (If You wish to be more geeky and do it 'old-style', press Ctrl + P and enter ext install Python folowed by Enter.)
Search for Python. You will see a lot of extensions. Every one of them has an information about author, number of downloads, rating and some description. Looking on number of downloads and rating, You will see that one of them is more distinguished - it's Don Jayamanne's Python extension (as for today it has more than 200k dwonloads). My suggestion is to use this extension (same as Microsoft's sugestion).
Just click install and restart VS Code if prompted.

An extension with rich support for Python language, with features including the following and more:

  • Linting (Prospector, PyLint, Pep8, Flake8, pydocstyle with config files and plugins)
  • Intellisense (autocompletion)
  • Auto indenting
  • Code formatting (autopep8, yapf, with config files)
  • Renaming, Viewing references, and code navigation
  • View signature and similar by hovering over a function or method
  • Excellent debugging suppot (variables, arguments, expressions, watch window, stack information, break points, remote debugging, mutliple threads)
  • Unit testing (unittests and nosetests, with config files)
  • Sorting imports
  • Snippets

VS Code is (almost) ready for Python development...

Create new workspace for VS Code: File -> Open Folder...

(You can use existing Python project or create empty folder as a new project - important thing is, that You need to open a Folder, not file, if You want to create a Workspace.)

Python env

So we have VS Code up and running with Python support. Now we need a properly configured Python env. What we want to have is:

  • Linting
  • PEP8 linting
  • Python3 runtime for testing our code without using external console/tool

As always, You have two options:

  • System-wide python env
  • virtual env

And we will touch both of them. Let start from system-wide env. I'm working on Linux Mint 17.3 (Ubuntu 14.04 LTS based) so the default Python version for me is 2.7 (2.7.6). I'm trying not to use 2.x. Everything I do in Python today is written for Python 3.x. There is of course Python 3 in Mint (3.4.3), but it is not default Python binary - You need to use python3 command to use it.
What I want to do, is to configure VS Code to use python3 as a default runtime for my code. I will run my code as 'Task' - You can configure any task and trigger it with Ctrl + Shift + B key combination. The Task can have args (which can be a variables like ${file} or ${fileBasename}).
(read more about Tasks) My 'Task' will trigger python3 command with one arg: ${fileBasename}. So 'Task' will run file I'm working on with command python3.

To configure Python task, You need to create a Task Runner. Press Ctrl + Shift + B key combination. You will get an information: "No task runner configured". Click "Configure Task Runner" and select "Other". An example Task Runner will be created - it'll be placed in .vscode directory in Your projects root directory as tasks.json file. You can remove all the content of this example Task Runner.

And here is my Task for Python:

{
    "version": "0.1.0",
    "command": "python3",
    "isShellCommand": true,
    "args": ["${fileBasename}"],
    "showOutput": "always"
}

Copy it all, paste to Your tasks.json file and save it (and close).

Now create new file in Your workspace with Ctrl + N key combination. Let's try a simple print() function:

print('Hello World!')

As You see, the code is not recognized as Python. Save it with Ctrl + Shift + S key combination, naming it as test.py. Now it is recognized as Python (You can see "Python" in right-bottom corner of the editor's window).

Now press the Ctrl + Shift + B key combination. The code will be executed with Task You configured and the output of the Task will be printed in bottom panel.

OK. So it works. But we are using system-wide python3 right now and probably we don't want to.

venv

Let's create venv in our workspace (You need to do it the way You like it - I will use a command line.

sudo pip install virtualenv
cd dev/Workspace
virtualenv -p /usr/bin/python3 venv
source venv/bin/activate

Now we are inside venv. We can install Python packages or configure them without changing system-wide Python. But we need to change our Tsk. If You want to edit tasks.json quickly, just press Ctrl + P and type tasks - tasks.json will show as first on dropdown list; click on it or hit Enter.

{
    "version": "0.1.0",
    "command": "venv/bin/python",
    "isShellCommand": true,
    "args": ["${fileBasename}"],
    "showOutput": "always"
}

(venv/bin/python is a symlink to venv/bin/python3)

Linters

One of the Python extension's for VS Code is ability to use Python linter and PEP8 Linter.

In computer programming, lint is a Unix utility that flags some suspicious and non-portable constructs (likely to be bugs).

In simple words: linter is checking Your code for bugs in real time. You don't need to use it but in most cases it is giving a lot of useful tips.

For Python linting and PEP8 linting we need two Python packages: pylint and pep8. I will install them inside my venv because I don't want them globally.

pip install pep8 pylint

Restart Your VS Code and let's code some bug:

print 'Hello World!'

As You see, there is a red underline. In bottom panel, select "Python" form dropdown list - You will se a linter output:

##########Linting Output - pylint##########************* Module test
1,0,error,E0001:Missing parentheses in call to 'print'

No config file found, using default configuration

And of course it's true, because in Python 3 print function needs to have brackets.

pylint, VS Code and venv

It is possible, that pylint did not worked for You. It's because linter in VS Code is using the default Python interpreter and we have installed pylint in venv (only tasks are executed with venv). The same situation is with other Python modeules and configurations that interacts with VS Code itself as an editor. How to use VS Code with venv then? Just call VS Code from inside of the venv. Close VS Code and then:

cd dev/Workspace
source venv/bin/activate
code .

It will run VS Code using venv.

PEP8

This is an output from pylint. Now we need to turn PEP8 linter ON.

From VS Code menu seelect File -> Preferences -> Workspace Settings.

In the left panel You will se the default configuration of VS Code. In the right panel, You can alter the defaults. In the default configuration there is an option:

// Whether to lint Python files using pep8
"python.linting.pep8Enabled": false,

So all we need to do is to copy this statement, paste it in the right panel, change false to true and save (remove the comma if it is the only cfg option in json).

{ "python.linting.pep8Enabled": true }

Restart VS Code and check Python output:

##########Linting Output - pep8##########1,15,W292,W292:no newline at end of file

##########Linting Output - pylint##########************* Module test
1,0,convention,C0304:Final newline missing
1,0,convention,C0111:Missing module docstring

No config file found, using default configuration

https://code.visualstudio.com
https://code.visualstudio.com/Docs/customization/keybindings
https://code.visualstudio.com/docs/editor/tasks
https://marketplace.visualstudio.com

comments powered by Disqus