DEV Community

Alvaro Cavalcanti
Alvaro Cavalcanti

Posted on

Setting Up a Python Remote Interpreter Using Docker

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 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.


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 for the necessary Docker files.

PyCharm Professional Edition

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

Visual Studio Code

  1. Install the Python extension
  2. Install the Remote - Containers extension
  3. Open the Command Pallette and type Remote-Containers, then select the Attach to Running Container... and selecet the running docker container
  4. VS Code will restart and reload
  5. On the Explorer sidebar, click the open a folder button and then enter /code (this will be loaded from the remote container)
  6. On the Extensions sidebar, select the Python extension and install it on the container
  7. When prompet on which interppreter to use, select /usr/local/bin/python
  8. Open the Command Pallette and type Python: Configure Tests, then select the unittest framework

Expected Results

  1. Code completion works
  2. Code navigation works
  3. Organize imports works
  4. Import suggestions/discovery works
  5. (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
  6. (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.

Top comments (0)