I'm trying to have python files format on save using black and neovim, with coc.
Coc config
I have the following coc config:
{
"coc.preferences.useQuickfixForLocations": true,
"coc.preferences.snippets.enable": true,
// python config
"python.venvFolders": [".virtualenvs"],
"python.venvPath" : "~/",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.jediEnabled": false,
"python.formatting.provider": "black",
"python.formatting.blackPath": "~/.virtualenvs/neovim/bin/black",
"coc.preferences.formatOnSaveFiletypes": ["python"]
}
Venv packages
Within the venv above I have the following black version:
-> % which python
/Users/x/.virtualenvs/neovim/bin/python
(neovim) x@y [14:31:38] [~/.virtualenvs]
-> % pip list | grep black
black 19.10b0
Other packages in this env are:
-> % pip list
Package Version
--------------- ---------
appdirs 1.4.4
attrs 19.3.0
black 19.10b0
click 7.1.2
greenlet 0.4.16
isort 5.1.4
jedi 0.17.2
msgpack 1.0.0
neovim 0.3.1
numpy 1.19.1
pandas 1.0.5
parso 0.7.1
pathspec 0.8.0
pip 20.1.1
pynvim 0.4.1
python-dateutil 2.8.1
pytz 2020.1
regex 2020.7.14
setuptools 41.2.0
six 1.15.0
toml 0.10.1
typed-ast 1.4.1
Behaviour / errors
I see no errors, but I see no results. For example I would expect the following to work given that setup is functioning:
# from
print('hi')
# to (having saved file)
print("hi")
I don't get this though, nothing happens.
Something unusual that does happen is that my buffer for a python file is
constantly in need of saving. By which I mean - I constantly have No write since last change
warning when trying to do :q
, which makes me think there's
something funny going on, but I don't know where.
Edit
Here is the output of :checkhealth
Edit 2
Black seems to be working, for me, these are settings that I currently have.
coc-settings.json
"python.formatting.provider": "black",
"python.formatting.blackPath": "~/.virtualenvs/neovim/bin/black",
"coc.preferences.formatOnSaveFiletypes": ["python"]
I also have nothing of the psf/black
plugin anymore, that was (i think)
causing some issues.
~
should not work. Anyway, have you solved the issue?~
works (doesn't have to be absolute path). Also, after a long day of investigating this workflow for myself, I finally managed to understand the setup and it works for now. Are you interested in an answer here? If so I'll try to write up a setup process as answer.