0

I activated the formatting on save in the init.el file uncommenting the following line

(format +onsave)  ; automated prettiness

But when I save a Python file, I get the following error message:

Error (before-save-hook): Error running hook "+format-buffer-h" because: (error You need the "black" command. You may be able to install it via "pip install black".)

I installed black with pip, as the message says, but I keep getting the same message.

I set the following settings for the Python black formatter (found it here):

;; package.el
(package! python-black)
;; config.el
(use-package! python-black
  :demand t
  :after python
  :config
  (add-hook! 'python-mode-hook #'python-black-on-save-mode)
  ;; Feel free to throw your own personal keybindings here
  (map! :leader :desc "Blacken Buffer" "m b b" #'python-black-buffer)
  (map! :leader :desc "Blacken Region" "m b r" #'python-black-region)
  (map! :leader :desc "Blacken Statement" "m b s" #'python-black-statement)
)

I did run doom sync after changing the settings, and it installed some packages, but I still get the same error when saving Python files.

I'm using Fedora 34, and installed black for Python 3.9. I also tried to install black for Python 2.7, but it's not supported anymore, so I was guessing that maybe Emacs is expecting black to be installed for Python 2.7, That's only a conjecture because I'm new to Emacs and don't know much about it.

Is there a way to make Python formatting work?

1 Answer 1

0

I just activated python and (format +onsave) only.
You must run doom/reload (SPC h r r) after doom sync.

Activate virtualenv with SPC : pyvenv-activate
Install black with python -m pip install black
Close open Python buffer and open again.

SPC b s to save buffer.
SPC c f to format buffer / region.

Run doom doctor to diagnose problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.