0

I want to run two actions whenever I save a Python file in PyCharm: first format the file with Black and then check its code style with Flake8.

I am doing this by setting up Black and Flake8 within PyCharm's File Watchers. This works, but Black and Flake8 appear to be run asynchronously, so I end up getting false positive warnings from Flake8 (style violations that have been corrected by Black).

How do I run the file watchers in sequence or trigger Flake8 to run once Black has finished?

Josh
  • 1,357
  • 2
  • 23
  • 45

1 Answers1

0

According to a JetBrains rep, it doesn't seem like this is supported (though that was in 2013).

Here is a workaround I created:

Create a batch file (pycharm-watcher):

#!/bin/bash

echo -e "Running Black ...\n"
black $1

echo -e "\nRunning Flake8 ...\n"
flake8 $1

Create a File Watcher:

Name: Black and Flake8
File type: Python
Scope: Project Files
Program: /path/to/pycharm-watcher
Arguments: $FilePath$
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$
Environment variables: (None)
Advanced Options: All unchecked
Show console: On error
Output filters: (None)

Ensure it is enabled in the File Watchers list, and set the level to Global (if you want it for all projects).

This will run Black and then Flake8 in sequence on the Python file whenever it is saved within PyCharm. It will only display the output if either Black or Flake8 result in error (e.g. code style violations).

Josh
  • 1,357
  • 2
  • 23
  • 45