Python — How to “Black” on Pycharm with docker without hassle in 3 min
In this post, I will show you how to simply setup your Pycharm (or Intelij ultimate) to format code using black via blackd
.
I’ll assume that you’re familiarized with Black. For more introductory content about this code formatter, please reach out the official site.
Prerequisites
- Do You have a PyCharm (or Intelij Ultimate)? ✅
- Do You have Docker installed? ✅
- Do You have Docker compose installed? ✅
So you’re good to go!
Now, How?
- Create a folder on your development environment: I suggest a folder called
tools/blackd
(because you can reuse it for other Python projects on your local machine as well) - Place a file named
docker-compose.yml
with the following content;
3. Start yourblackd
service running the following command: $ docker compose up -d
REMARK: -d
in that command means “run in background” 😉
4. Open your Pycharm; Go under settings -> plugins and install “BlackConnect”;
5. Now the “BlackConnect” menu will be available. (If it doesn’t show for you, restart your IDE and then it will be available)
6. Configure “BlackConnect” pointing to your newly created container;
Click on “Check connection”; it should be Ok.
Last, but not least, check the “Trigger Settings” as you please. I recommend to select this Trigger when saving changed files
. It self explanatory though. 😅
Wrapping up
So, that’s it. Please leave a comment if you want to have more content like this. If you have more questions like that, please let me know.
Thank you for your time!