Which Python Code Formatter – Black vs Blue

Introduction

Which Python Code Formatter - Black vs Blue
Which Python Code Formatter – Black vs Blue

We will go over the differences between Which Python Code Formatter – Black vs Blue.

Did you know that Poetry uses PIP under the hood but the abstraction makes it a different system that’s comparable?

I’ve always used PIP in my Python projects but the last few months I was introduced to Poetry and I’ve been liking it a lot. I think it has it’s place so I wanted to write about it and see how it compares with PIP and whether or not it’s a good fit for your project.

We will break down this in the following sections:

  • What is the Difference between Poetry and PIP
  • Is Poetry A Replacement for PIP
  • Detailed Comparison PIP vs Poetry

I have used both PIP and Poetry successfully in various projects and they both work like a charm. I believe both have a place in my heart but which one do I use the most and why? I will be breaking down this and you can make your own mind up.

We will go point by point on how both compare in various tasks and what is the advantage of each of them.

Quick Comparisson

If you don’t want to spend a lot of time going through the differences between the two, I made a quick comparison table for you to go through and see the differences between the two and hopefully make a choice for yourself.

For more detailed information and how these two compare you will need to keep reading on the sections below as I answer very popular questions and I go into depth on their differences so you can make a more educated choice for your next project.

BlueBlack
InstallationEasyAverage
Ease Of UseEasyEasy
CustomizationGoodAverage
SecurityGoodGood
SpeedFastFast
IntegrationGoodGood
FeaturesGoodAverage
PopularityLowGood

To beautify code in Python you need to make use for a code formatter. Basically a code formatter takes as input your Python code and produces as output a standard formatted Python script. The idea behind this is that your code looks consistently the same across the board at least a syntactic level. There’s many advantages to doing this which we will outline below.

Basically your code looks prettier and more beautiful along with easier to read for some third party. When you have consistency and things are laid out nicely on the editor it makes it easier to debug and understand as well.

Detailed Comparison For Python Code Formatters

Now that we went over some definitions lets start diving into the deep waters and break down each feature to help you decide which solution is better for you. Once again keep in mind that below we will be focusing on the differences between Python Black and Blue. I believe those two projects have a lot of potential and will probably dictate the goto standard for most people looking to prettify, beautify and format their Python code.

In this review we will be comparing `

Installation / Update

Python Code Formatters - Installation
Python Code Formatters – Installation

The first section we will be covering is the installation and update process of the package itself. Both of these packages provide very simple ways of doing it but let’s go more into the specifics to fully understand this process.

First you need to keep in mind that blue uses under the hood black. As you will see in the installation below the first dependency it installs is black. Having said that both packages are very easy to install and deploy to your system.

All you will need to have as a pre-requisite is the PIP (Python package manager). I illustrate below the commands used to install blue.

$ pip install blue
Collecting blue
  Downloading blue-0.9.0-py3-none-any.whl (10 kB)
Collecting black==22.1.0
  Downloading black-22.1.0-cp39-cp39-macosx_11_0_arm64.whl (1.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 7.0 MB/s eta 0:00:00
Collecting flake8>=3.8
  Downloading flake8-4.0.1-py2.py3-none-any.whl (64 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.1/64.1 KB 2.2 MB/s eta 0:00:00
Collecting mypy-extensions>=0.4.3
  Downloading mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting tomli>=1.1.0
  Downloading tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting platformdirs>=2
  Downloading platformdirs-2.5.2-py3-none-any.whl (14 kB)
Collecting typing-extensions>=3.10.0.0
  Using cached typing_extensions-4.2.0-py3-none-any.whl (24 kB)
Collecting click>=8.0.0
  Downloading click-8.1.3-py3-none-any.whl (96 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 KB 3.1 MB/s eta 0:00:00
Collecting pathspec>=0.9.0
  Downloading pathspec-0.9.0-py2.py3-none-any.whl (31 kB)
Collecting pycodestyle<2.9.0,>=2.8.0
  Downloading pycodestyle-2.8.0-py2.py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.1/42.1 KB 1.3 MB/s eta 0:00:00
Collecting mccabe<0.7.0,>=0.6.0
  Downloading mccabe-0.6.1-py2.py3-none-any.whl (8.6 kB)
Collecting pyflakes<2.5.0,>=2.4.0
  Downloading pyflakes-2.4.0-py2.py3-none-any.whl (69 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 69.7/69.7 KB 2.3 MB/s eta 0:00:00
Installing collected packages: mypy-extensions, mccabe, typing-extensions, tomli, pyflakes, pycodestyle, platformdirs, pathspec, click, flake8, black, blue
Successfully installed black-22.1.0 blue-0.9.0 click-8.1.3 flake8-4.0.1 mccabe-0.6.1 mypy-extensions-0.4.3 pathspec-0.9.0 platformdirs-2.5.2 pycodestyle-2.8.0 pyflakes-2.4.0 tomli-2.0.1 typing-extensions-4.2.0

And to go ahead and install black we follow a similar process. Since blue relies on black you will notice that all the dependencies are already satisfied since it was already installed.

$ pip install black
Requirement already satisfied: black in ./venv/lib/python3.9/site-packages (22.1.0)
Requirement already satisfied: click>=8.0.0 in ./venv/lib/python3.9/site-packages (from black) (8.1.3)
Requirement already satisfied: platformdirs>=2 in ./venv/lib/python3.9/site-packages (from black) (2.5.2)
Requirement already satisfied: tomli>=1.1.0 in ./venv/lib/python3.9/site-packages (from black) (2.0.1)
Requirement already satisfied: pathspec>=0.9.0 in ./venv/lib/python3.9/site-packages (from black) (0.9.0)
Requirement already satisfied: typing-extensions>=3.10.0.0 in ./venv/lib/python3.9/site-packages (from black) (4.2.0)
Requirement already satisfied: mypy-extensions>=0.4.3 in ./venv/lib/python3.9/site-packages (from black) (0.4.3)

There is however an issue with the installation of Python black out of the box and this is that it doesn’t work. To test it we simply go ahead and run the black command and we will notice an exception for the click package.

$ black
Traceback (most recent call last):
  File "/Users/alex/code/unbiased/python-find-images-in-page/venv/bin/black", line 8, in <module>
    sys.exit(patched_main())
  File "src/black/__init__.py", line 1423, in patched_main
  File "src/black/__init__.py", line 1409, in patch_click
ImportError: cannot import name '_unicodefun' from 'click' (/Users/alex/code/unbiased/python-find-images-in-page/venv/lib/python3.9/site-packages/click/__init__.py)

After doing some research online I found out that the package depends on a specific version of click which is 8.0.2 at the time of this writing. So in order to resolve the problem we go ahead and install the click package with the specific version that’s compatible with the Python black package.

You can do this by following the instructions below and this would resolve your dependency on running Python black.

$ pip install --upgrade click==8.0.2
Collecting click==8.0.2
  Downloading click-8.0.2-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.6/97.6 KB 1.7 MB/s eta 0:00:00
Installing collected packages: click
  Attempting uninstall: click
    Found existing installation: click 8.1.3
    Uninstalling click-8.1.3:
      Successfully uninstalled click-8.1.3
Successfully installed click-8.0.2

This is all good but to me this says one important thing about the maintainer of the package Python black. It has not been tested extensively as a result we had this issue. Granted this may and will probably get resolved in the future but the fact that it wasn’t tested well to begin with means the maintainers did not do their job well.

While I don’t like pointing the blame at people since we have all faced these Python dependency issues and sometimes the package manager is at fault we still need to acknowledge the reality of things and accept that there was an issue. For this reason I would give the installation process and updating to Python Blue code formatter.

Winner: Blue

Ease Of Use

Python Code Formatters - Ease Of Use
Python Code Formatters – Ease Of Use

When it comes to a code formatter you want to get the job right and you want to get it done fast. So ease of use is a very important factor to using the Python code formatting tool of choice.

In my tests and as it can be seen below both were very easy to use and got the job don’t effortlessly. In both cases if you are happy with the defaults you can just run the command and it will format your Python code found in the running directory. This makes it super easy and easily deployable and integratable to your system build and setup scripts or even in automation at the git level such as hooks.

$ black .
reformatted python-extract-images-page.py

All done! ✨ ? ✨
1 file reformatted.

Similarly blue will follow black under the hood and format your code.

$ blue .
reformatted python-extract-images-page.py

All done! ✨ ? ✨
1 file reformatted.

As you can do customizations in Blue and adjust certain things and this involves an extra step as making a configuration I would give this one to Python Black code formatter.

Winner: Black

Security

PIP vs Poetry - Security
Python Code Formatters – Security

When it comes to security generally speaking both packages are fairly secure since they run through PIP and the Python ecosystem.

However if we take a closer look at the installation process earlier we will note that Python Blue has more dependencies to external packages which makes it more open to attacks. Generally speaking the more libraries you include in your project to make things work the higher the attack vector.

Since both are minimal in my opinion again this will not affect most people, but if you are in a strict secure environment and you need to have the best security then go with Python black. To top things up Black has some changes planned coming soon which will improve security by adding more pedantic checks on the versioning of the packages. I also think you can easily integrate it based on this with some external tool such as white source to be even more clear on the security state of your dependencies.

Winner: Black

Speed

Python Code Formatters - Speed
Python Code Formatters – Speed

I tested both in big projects and I found that Blue was slightly slower. I believe this comes at the cost of having extra features thus more processing and things to do.

Since this is a one time job for most projects it may not be an issue for you especially if you have one of the following reasons below:

  • Have a small code base so things finish fast anyway
  • You are not re-deploying and re-packaging your project in containers or build systems
  • You do not have any issues running this as a batch job in your git hooks
  • You generally don’t mind waiting a few seconds more

Overall I believe for 99% of the people the speed is not going to be an issue but if you are an exception and fall into one of the reasons of having to do this very frequently then I’d say go with Black as it’s simpler and faster.

Winner: Black

Integration / Customization

Python Code Formatters - Integration
Python Code Formatters – Integration / Customization

Blue supports configuration in various formats:

  • pyproject.toml
  • setup.cfg
  • tox.ini
  • .blue

This allows great customization and configuration options unlike black which is more rigid to the standard. In my opinion having the flexibility to do things is a very important factor. There are standards for a good reason and following PEP8 is great if you are a new programmer and do not have much experience.

However if you are more seasoned and have used Python extensively and know what you need from the language there’s no need to follow the PEP8 standard if it does not confront to your needs. Being flexible and having options is always better, otherwise you can always revert back to the defaults and let it to do it’s thing.

    Winner: Blue

    Features

    Python Code Formatters - Features
    Python Code Formatters – Features

    When it comes to features I’d say this is more of a preference of how each of them works.

    Simply put if you are a double quotes fan go with Python Black otherwise if you are like me which likes single quotes then go with Python Blue. Furthermore I do need to note here for docstrings Python blue uses double quotes too! While this may seem strange I find it more eye appealing and I agree with their decision. So subjectively speaking I’m completely aligned with Python Blue on their philosophy and project choices they have made.

    Since this is completely subjective your preference may be Python Black and I completely understand this. After all we are all in one side of the court when it comes to quotes and our code. To illustrate this more specifically lets see how this would look if we were to format one of my source codes.

    The example below shows how Python black changed my single quote strings to double quote.

    -    if 'http' != url[:4]:
    +    if "http" != url[:4]:
             turl = page + url
         else:
             turl = url
    
    -    print(f'Downloading file: {turl}')
    +    print(f"Downloading file: {turl}")

    Similarly if I run Python Blue into the same code this will get translated from double quotes to single quotes and this will make all the difference in the world at least form.

    -    if "," in url:
    +    if ',' in url:
             return
    -    if "http" != url[:4]:
    +    if 'http' != url[:4]:
    
    - print(f"Downloading file: {turl}")
    + print(f'Downloading file: {turl}')

    One thing is certain here whatever tool you decide to use in your Python Code Formatting venture make sure you are consistent across the board. The last thing you want to do is have code that’s mixed between double and single quotes. This makes readability harder and also consistency goes down the drain for someone trying to adopt best practises. Worst case if uncertain add one of the two in your git hooks in case a programmer forgets to follow your standard.

    Winner: Personal Choice

    Popularity

    Python Code Formatters - Popularity
    Python Code Formatters – Popularity

    If we check the graph below which is over a span of 12 month period you will notice that Python Black is by far more popular than Python Blue. This is because the project is older and it conform with the PEP8 standard. Also Blue relies on Python Black so whoever is in the quest for a Python Code Formatter will first examine the goto solution which is Python Black.

    Python Code Formatters - Trends
    Python Code Formatters – Trends

    In my opinion Python Blue will gain more traction over time as it becomes more popular and users get awareness of what it offers to you as a user. Since the majority users in Python are new people starting out Python Black will always be the choice of preference for them so this will still get a big chunk of the pie.

    Winner: Black

    Is There A Prettier For Python

    Currently there is no prettier version for Python. Since both Black and Blue solve a similar problem and make your code pretty they are good alternatives. I recommend giving those a try and see if you want to use them in your project.

    Earlier in this article we went over the advantages and disadvantages of each of the Python code formatters to help you decide which is suitable for you. Prettifying your Python code is super easy with the above tools.

    Conclusion

    We were able to successfully go over Which Python Code Formatter – Black vs Blue, hopefully I answered any questions you may have had and helped you get started on your quest of selecting the right Python Syntax coding tool.

    If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.

    If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.

    Would you consider using an automated code formatter for your Python code?

    I personally think both have their place in the right project. It really depends on what you want to accomplish and how you want to go about it. Even though I love black I will still keeping an open mind and checking on Python Blue.

    If you would like to visit the official Python Black documentation you can find it here.

    If you would like to visit the official Python Blue documentation you can find it here.

    If you would like to find more articles related to Python:

    Leave a Comment

    Your email address will not be published. Required fields are marked *