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:
Use Black in Terminal to Reformat Code File
Have a look at this horrible code in the file
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.
The resulting code file now looks much prettier:
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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.