Setup PyCharm to use a Project Interpreter in a Docker Compose Service

Mar 23, 2019

Overview

If you are anything like me, you heavily rely on a debugger. Likewise, you install all your development environments into a docker container because installing things on your own computer is FOR THE BIRDS. I cannot tell you what a relief it is to have all my development environments safely in docker containers. I break operating systems frequently. It's a serious problem for me and I needed an intervention. For me, this intervention was docker. I do not care how often I break a docker container, because 1. it's versioned, and 2. I can always just rebuilt it. MAGICAL.

The Problem

Many IDEs and editors assume you are working from a local environment. I think this is changing with so many people moving towards cloud computing and technologies like docker. I don't have an exact list of IDEs that do and do not have use an interpreter from a docker or docker-compose environment. 

The Solution (Well, a solutions anyways! )

The JetBrains IDEs support a whole host of execute code somewhere besides your computer. I've been using the JetBrains IDEs, namely PyCharm and WebStorm, and occasionally one of the others when I am supporting a researcher in some other language. I really love them, namely because of their debuggers, which are amazing, the fact that you can increase the memory the IDE can use for intensive applications, such as data science applications, and they several 'setup your interpreter to be someplace else' options. I only care about Docker and SSH, but if you use Vagrant they have that too. The only downside is that they are paid, but I feel that they absolutely save me enough time to justify the price tag. Time is money!

Quick disclaimer, no I am not being paid to talk about JetBrains IDEs. I just think they are an extremely high quality set of tools. Again, I think other IDEs may have this same functionality, but I use PyCharm so there we go.

SetUp PyCharm for Docker-Compose

PyCharm works by having separate projects. Each project has an interpreter, which has its associated python packages. If you need to step through the code of any of these libraries than of course you can, in addition to running tests from any of these libraries.

Add the Project Interpreter from Docker-Compose

 Whenever I need to search for anything in PyCharm, either functionality of the IDE itself, class definitions, or tests, I simply double press Shift. This brings up a search menu and is by FAR the easiest way to navigate the menus. I actually find the menus themselves a bit confusing, but that search box is great.

So, double press Shift to bring up the search menu, and start typing 'Project Interpreter'. You probably won't even need to type the whole thing to get the correct menu option.

You want to press the down key, or up key as the case may be, until you hit the menu option 'Project Interpreter: Preferences -> Project Interpreter'. Press Enter, which will bring you to the Project Interpreter. 

Now press the settings toggle, and click 'Add'.

Mine already had packages populated because I had done this previously, but you get the idea.

Once you've clicked 'Add' you will have a few different options. In this case you want the 'Docker Compose' option. Click on that, and you will see that you need to tell PyCharm where you docker-compose file is (relative to your project root), and choose the service you want. 

If you are using your own docker-compose.yml file you will need to choose whichever service has python on it. For the example above that is either airflow_scheduler, airflow_worker, or airflow_webserver. Your service will need to be up and running (docker-compose up). If your desired python in your PATH you can leave everything as default. Otherwise you will need to hard code the Python Interpreter Path below.

 

That's it! Press Ok (not pictured here) and you will see the python packages start to populate under your chosen project interpreter.

WrapUp

That's it! I hope you found this post useful. If you did, and know someone else who might, please share!

Bioinformatics Solutions on AWS Newsletter 

Get the first 3 chapters of my book, Bioinformatics Solutions on AWS, as well as weekly updates on the world of Bioinformatics and Cloud Computing, completely free, by filling out the form next to this text.

Bioinformatics Solutions on AWS

If you'd like to learn more about AWS and how it relates to the future of Bioinformatics, sign up here.

We won't send spam. Unsubscribe at any time.