Setting Up a Python Remote Interpreter Using Docker
Tags: python, pycharm, vscode, tdd
Published: 2019-11-08 13:28:19

# Why a Remote Interpreter instead of a Virtual Environment?

A well-known pattern in Python (and many other languages) is to rely on virtual environment tools (`virtualenv`, `pyenv`, etc) to avoid the [SnowflakeServer](https://martinfowler.com/bliki/SnowflakeServer.html) anti-pattern. These tools create an isolated environment to install all dependencies for any given project.

But as of today there's an improvement to that pattern, which is to use Docker containers instead. Such containers provide much more flexibility than virtual environment, because they are not limited to a single platform/language, instead they offer a fully-fledged virtual machine. Not to mention the `docker-compose` tool where one can have several containers interacting with each other.

This article will guide the reader on how to set up the two most used Python IDEs for using Docker containers as remote interpreters.

## Pre-requisites

A running Docker container with:

- A volume mounted to your source code (henceforth, `/code`)
- SSH setup
- SSH enabled for the `root:password` creds and the root user allowed to login

Refer to [this gist](https://gist.github.com/alvarocavalcanti/24a6f1470d1db724a398ea6204384f00) for the necessary Docker files.

## PyCharm Professional Edition

1. Preferences (CMD + ,) > Project Settings > Project Interpreter
1. Click on the gear icon next to the "Project Interpreter" dropdown > Add
1. Select "SSH Interpreter" > Host: localhost, Port: 9922, Username: root > Password: password > Interpreter: /usr/local/bin/python, Sync folders: Project Root -> /code, Disable "Automatically upload..."
1. Confirm the changes and wait for PyCharm to update the indexes

## Visual Studio Code

1. Install the [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) extension
1. Install the [Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension
1. Open the Command Pallette and type `Remote-Containers`, then select the `Attach to Running Container...` and selecet the running docker container
1. VS Code will restart and reload
1. On the `Explorer` sidebar, click the `open a folder` button and then enter `/code` (this will be loaded from the remote container)
1. On the `Extensions` sidebar, select the `Python` extension and install it on the container
1. When prompet on which interppreter to use, select `/usr/local/bin/python`
1. Open the Command Pallette and type `Python: Configure Tests`, then select the `unittest` framework

## Expected Results

1. Code completion works
1. Code navigation works
1. Organize imports works
1. Import suggestions/discovery works
1. (VS Code) Tests (either classes or methods) will have a new line above their definitions, containing two actions: `Run Test | Debug Test`, and will be executed upon clicking on them
1. (PyCharm) Tests (either classes or methods) can be executed by placing the cursor on them and then using `Ctrl+Shift+R`

## Bonus: TDD Enablement

One of the key aspects of the Test-Driven Development is to provide a short feedback on each iteration (write a failing test, fix the test, refactor). And a lot of times a project's tooling might work against this principle, as it's fairly common for a project to have a way of executing its test suite, but it is also common that this task will run the entire suite, not just a single test.

But if you have your IDE of choice able to execute just a single test in a matter of seconds, you will feel way more comfortable on given TDD a try.