{"id":101651,"date":"2019-10-11T08:00:57","date_gmt":"2019-10-11T08:00:57","guid":{"rendered":"https:\/\/fedoramagazine.org\/?p=29445"},"modified":"2019-10-11T08:00:57","modified_gmt":"2019-10-11T08:00:57","slug":"make-your-python-code-look-good-with-black-on-fedora","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/10\/11\/make-your-python-code-look-good-with-black-on-fedora\/","title":{"rendered":"Make your Python code look good with Black on Fedora"},"content":{"rendered":"<p>The Python programing language is often praised for its simple syntax. In fact the language recognizes that code is read much more often than it is written. <a href=\"http:\/\/github.com\/psf\/black\">Black<\/a> is a tool that automatically formats your Python source code making it uniform and compliant to the <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\/\">PEP-8 style guide<\/a>.<\/p>\n<p> <span id=\"more-29445\"><\/span> <\/p>\n<h2>How to install Black on Fedora<\/h2>\n<p>Installing Black on Fedora is quite simple. Black is maintained in the official repositories.<\/p>\n<pre class=\"wp-block-preformatted\">$ sudo dnf install python3-black<\/pre>\n<p>Black is a command line tool and therefore it is run from the terminal.<\/p>\n<pre class=\"wp-block-preformatted\">$ black --help<\/pre>\n<h2>Format your Python code with Black<\/h2>\n<p>Using Black to format a Python code base is straight forward.<\/p>\n<pre class=\"wp-block-preformatted\">$ black myfile.py\nAll done! <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora-1.png\" alt=\"\ud83c\udf70\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> 1 file left unchanged.\n$ black path_to_my_python_project\/\nAll done! <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora-1.png\" alt=\"\ud83c\udf70\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/>\n165 files reformatted, 24 files left unchanged.<\/pre>\n<p>By default Black allows 88 characters per line, meaning that the code will be reformatted to fit within 88 characters per line. It is possible to change this to a custom value, for example :<\/p>\n<pre class=\"wp-block-preformatted\">$ black --line-length 100 my_python_file.py<\/pre>\n<p>This will set the line length to allow 100 characters.<\/p>\n<h2>Run Black as part of a CI pipeline<\/h2>\n<p>Black really shines when it is integrated with other tools, like a continuous integration pipeline.<\/p>\n<p>The <em>&#8211;check<\/em> option allows to verify if any files need to be reformatted. This is useful to run as a CI test to ensure all your code is formatted in consistent manner.<\/p>\n<pre class=\"wp-block-preformatted\">$ black --check myfile.py\nwould reformat myfile.py\nAll done! <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora-2.png\" alt=\"\ud83d\udca5\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora-3.png\" alt=\"\ud83d\udc94\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/> <img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/10\/make-your-python-code-look-good-with-black-on-fedora-2.png\" alt=\"\ud83d\udca5\" class=\"wp-smiley\" style=\"height: 1em;max-height: 1em\" \/>\n1 file would be reformatted.<\/pre>\n<h2>Integrate Black with your code editor<\/h2>\n<p>Running Black during the continuous integration tests is a great way to keep the code base correctly formatted. But developers really wants to forget about formatting and have the tool managing it for them. <\/p>\n<p>Most of the popular code editors support Black. It allows developers to run the format tool every time a file is saved. The <a href=\"https:\/\/black.readthedocs.io\/en\/stable\/editor_integration.html\">official documentation<\/a> details the configuration needed for each editor. <\/p>\n<p>Black is a must-have tool in the Python developer toolbox and is easily available on Fedora.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Python programing language is often praised for its simple syntax. In fact the language recognizes that code is read much more often than it is written. Black is a tool that automatically formats your Python source code making it uniform and compliant to the PEP-8 style guide. How to install Black on Fedora Installing [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":101652,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[467,108,45,42,46,47,468],"class_list":["post-101651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fedora-os","tag-developers","tag-development","tag-fedora","tag-for-developers","tag-magazine","tag-news","tag-python"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/101651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=101651"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/101651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/101652"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=101651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=101651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=101651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}