9

I am trying to setup the formatter "black" to work with PyCharm. I have tried setting it up with both the standard External Tools preferences and using the Files Watcher Plugin. Neither has worked for me.

I am getting these errors:

PyCharm Errors in the run console

I have it installed in /Users/Scott/Library/Python/3.8/lib/python/site-packages/black.py and want it to run globally for all Python project files when I save.

This is how my preferences look:

PyCharm External Tools configuration for the black formatter

I am new to Python and PyCharm and Stack Overflow for that matter, and am stilling setting this up. I appreciate your help.

Scott K.
  • 191
  • 1
  • 2
  • 12
  • 2
    Consider using plugins, e.g. https://plugins.jetbrains.com/plugin/10563-black-pycharm or https://plugins.jetbrains.com/plugin/14321-blackconnect – user2235698 Jun 19 '20 at 22:25

2 Answers2

12

Your problem is that you use .py file as a command. You need to add python to tell which program can execute this code.

Alternatively, you can use black executable if it is installed in your python environment.

enter image description here

stasdeep
  • 2,758
  • 1
  • 16
  • 29
  • 1
    this works, but how can i trigger this to run on save? – szaman Nov 27 '21 at 08:42
  • I believe this is not implemented in PyCharm, but you can try a trick like this: https://marcesher.com/2014/03/30/intellij-idea-run-goimports-on-file-save/ – stasdeep Nov 30 '21 at 05:35
0

You have to install Black using pip install black[d] not from pycharm console in a project but at your python directory then install from Pycharm>file>settings..>plugins, (BlackConnect plugin), and it will work for sure. about trigger this on save.

Pycharm Settings
Pycharm Settings

or press shift + alt + b

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
M MK K
  • 1