How to Install and Use Black in PyCharm?

Black is a PEP 8 compliant code formatter that will automatically improve your code file in terms of style and adherence to the Python standard. It’ll make your code more Pythonic!

Install Black using PyCharm Menu

To install Black in PyCharm, go to Settings > Project: Your Project > Python Interpreter > Click the “+” symbol to add a new library > Type in “black” and install the first result that comes up.

Install Black Using PyCharm Terminal

Alternatively, you can also run pip install black in your PyCharm Terminal view:

$ pip install black

Note that the “$” symbol indicates that this is a shell script – don’t copy&paste it!

Here’s how installing Black with pip looks in my PyCharm environment on a small toy project:

"pip install black" on PyCharm IDE

Use Black in Terminal to Reformat Code File

Have a look at this horrible code in the file "main.py":

Before formatting with black (PyCharm)

To automatically reformat a given code file, use the command black filename in your Terminal—providing the full path to the filename relative to the Terminal’s current path. After running the command black filename, the file now looks much prettier and consistent—adhering to many of the PEP 8 syntax rules.

black main.py

The resulting code file now looks much prettier:

Black Reformatting PyCharm

Black automatically converts ugly, non-Pythonic code into beautiful, consistent, and Pythonic code that is compliant with Python’s PEP 8 code rules. What a useful tool! 🥰

To boost your coding skills, feel free to check out our cheat-sheet-based Python email academy: