Wednesday, December 7, 2022

Format your python code using “black”

Black Logo

The python style guide PEP 8 by the creator of the language Guido van Rossum is the universal standard that most python programmers strive to follow.  There are many modules that can format python code to make it compliant with the style guide, but recently one of them, “black” is rapidly becoming the standard.

It is very easy to integrate this module with PyScripter. 

  1. First you need to install it using Tools, Tools, Install Packages with pip
  2. Then, use the menu commands Tools, Configure Tools, Add to add a new external tool
  3. Fill in the dialog box as indicated below

 

black

Set:

Name: Format Selection

Description: Format selected code using the “black” module

Command: $[PythonExe-Short]

 

Name: Format Selection
Description: Format selected code using the “black” module
Command: $[PythonExe-Short]
Parameters: -m black -
Shortcut: Shift+Alt+F
Context: Selection Available
Std. Input: Selection
Std. Ouput Selection

Also check “Hide console” and “UTF8 IO” and press  OK.

You are all set.   Select some code or press Ctrl+A to select the whole file and invoke the Format Selection command using the shortcut or via the Tools menu.

This tool definition is available with release 4.2.2 in new installations, but you need to add it manually to existing installations.

No comments: