Table of Contents

configure pycharm to format code with black

To run black on the entire file

File -> Settings
-> Tools -> External Tools -> click on + sign
-> Name: Black
   Description: Black in pycharm configuration
   Program: C:\ProgramData\Continuum\Anaconda\envs\[ENV_NAME]\Scripts\black.exe
   Arguments: $FilePath$
   Working directory: $ProjectFileDir$

To run black on selected text

File -> Settings
-> Tools -> External Tools -> click on + sign
-> Name: Black Selected
   Description: run black on selected text
   Program: "C:\Program Files\Git\bin\bash.exe"
   Arguments: --login H:\work\myrepos\rutils\python3\black_on_selected.sh C:\ProgramData\Continuum\Anaconda\envs\ro_udw_env\Scripts\black.exe $FilePath$ $SelectionStartLine$ $SelectionEndLine$
   Working directory: $ProjectFileDir$

Restart pycharm after adding the external tools.

To format code

Select code of interest
-> Tools -> External Tools -> Black Selected

See also :-

do not convert single quotes to double quotes

pass --skip-string-normalization on the command line.

Ref:-