Automatically run Black in PyCharm on Windows (December 2022)

Jostein Leira
2 min readDec 21, 2022

--

Updated 2022–12–21

This is an updated version of Automatically run black in PyCharm on Windows (December 2021)

I often forget to run the code formatter before I commit code to git. This is bad since the following commits will contain irrelevant formatting changes.

I want the code formatter to run automatically. I have heard about git pre-commit hooks but never used them on Windows.

These are the installation steps:

  1. Install black in your project’s virtual environment
  2. Enable the File Watchers plugin in PyCharm

Install black in your project’s virtual environment

Install black in your current project’s virtual environment.

Enable the File Watchers plugin in PyCharm

The File Watchers plugin is bundled with PyCharm. First, we need to enable the plugin. Choose the menu option Settings | Plugins | File Watchers | Enable.

Then create a new file watcher by going to the menu option Settings | Tools | File Watchers | + (Add <custom> template).

Fill in the fields and replace the Program with your correct path to the Scripts folder. Add black at the end:

Name: Black
File type: Python
Scope: Project Files
Program: $PyInterpreterDirectory$/black
Arguments: "$FilePath$"
Output paths to refresh: "$FilePath$"
Uncheck all advanced options.

The File Watchers plugin will run black on your files when they change.

Sources

Black documentation: Black Editor integration

The PyCharm logo Copyright © 2000–2022 JetBrains s.r.o. JetBrains and the JetBrains logo are registered trademarks of JetBrains s.r.o.

The Black logo Copyright © 2018 Łukasz Langa

The Windows logo Copyright © 2021 Microsoft

--

--

Jostein Leira

Python software developer and Google Cloud Certified Professional Cloud Architect