Alvaro Cavalcanti
Posted on November 8, 2019
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.
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 for the necessary Docker files.
PyCharm Professional Edition
- Preferences (CMD + ,) > Project Settings > Project Interpreter
- Click on the gear icon next to the "Project Interpreter" dropdown > Add
- Select "SSH Interpreter" > Host: localhost, Port: 9922, Username: root > Password: password > Interpreter: /usr/local/bin/python, Sync folders: Project Root -> /code, Disable "Automatically upload..."
- Confirm the changes and wait for PyCharm to update the indexes
Visual Studio Code
- Install the Python extension
- Install the Remote - Containers extension
- Open the Command Pallette and type
Remote-Containers
, then select theAttach to Running Container...
and selecet the running docker container - VS Code will restart and reload
- On the
Explorer
sidebar, click theopen a folder
button and then enter/code
(this will be loaded from the remote container) - On the
Extensions
sidebar, select thePython
extension and install it on the container - When prompet on which interppreter to use, select
/usr/local/bin/python
- Open the Command Pallette and type
Python: Configure Tests
, then select theunittest
framework
Expected Results
- Code completion works
- Code navigation works
- Organize imports works
- Import suggestions/discovery works
- (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 - (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.
Posted on November 8, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.