DEV Community

10x learner
10x learner

Posted on • Originally published at 10xlearner.com on

2 1

Formatting Python – Why and How !

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain why you should format you python scripts and how to do it.

Self promotion: Here are a few social networks where you can follow me and check my work as a programmer and a writer 😉

Personal website, Twitter, Dev.to, CodeNewbie, Medium, GitHub

Problematic (or why you should do it)

If you are not convinced that you should format you code, I really encourage you to go look at the previous article I did on Formatting and Automation, where I go in detail on why you should definitively format your code, and do it in a automatic way, to make your code more readable and make your team more productive since they won’t have to spend time arguing about Spaces VS Tabs.

Solution (or how to do it)

The short answer, for the people who don’t want to read through the entire article (I know you do that! I do it too 😆) is to use the tool named black:

# Installation of the formatting tool on any platform
pip install black # with python3 !

# Run the formatting tool on a python script
black <path/to/python/script.py>
Enter fullscreen mode Exit fullscreen mode

In the code above, we start by installing the tool black, which is described as "The uncompromising code formatter". I only showed you the simplest way to use this tool, by passing it, as arguments the list of the files you want to format, but there are a lot of other options available to customize the formatting process, check if the code is already formatted, for example.

You can find all the details in the Black documentation which is really complete and easy to read. 😉

On important detail about this tool is that you can use it on a folder and it will recursively go though the sub-folders and format all the python files it will find. So, if you want, you can pass it the root directory containing all the files you want to format, and black will do the rest 😉


Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay