black: Black Vim Error Formatting File: Error invoking `python_execute`
Describe the bug A clear and concise description of what the bug is.
When saving/formatting a python file via :Black in Vim, it produced the following error:
Error detected while processing function provider#python3#Call:
line 18:
Error invoking 'python_execute' on channel 4 (python3-script-host):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 92, in Black
File "<string>", line 128, in get_configs
AttributeError: module 'black' has no attribute 'find_pyproject_toml'
To Reproduce Steps to reproduce the behavior:
- Open up some python file, writing something, and leave insert mode
- Type
:Blackand hit Enter to save - Produce the error above
Expected behavior Python code should be formatted without errors
Environment (please complete the following information):
- Version: 19.10b0
- OS and Python version: macOS 10.14.6 / Python 3.7.5
Does this bug also happen on master? To answer this, you have two options: No I don’t think so. Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 82
- Comments: 37 (8 by maintainers)
Commits related to this issue
- Freeze black version until https://github.com/psf/black/issues/1293 is resolved — committed to carno/dotfiles by carno 4 years ago
- black.vim: Tag at 19.10b0 Avoid issue here: https://github.com/psf/black/issues/1293 — committed to tony/vim-config-framework by tony 4 years ago
- Use black from coc.nvim If black version in site-packages isn't master, will fail, so use the coc.nvim format for black See also: https://github.com/psf/black/issues/1293 — committed to tony/vim-config-framework by tony 4 years ago
- Bring back black and isort Also rm -rf ~/.vim/black rm -rf ~/.vim/plugged/black https://github.com/psf/black/issues/1293 https://github.com/psf/black/pull/1318/files — committed to tony/vim-config-framework by tony 4 years ago
- vim: fix for black formatter Pins Black plugin version to tag 19.10b0 which is equal to the Arch package version. See also <https://github.com/psf/black/issues/1293>. Signed-off-by: Tibor Šimko <tib... — committed to tiborsimko/dotfiles by tiborsimko 4 years ago
- vim black workaround See also: https://github.com/psf/black/issues/1293#issuecomment-621082845 — committed to tony/vim-config-framework by tony 4 years ago
- vim: Use isort and black via system command (the plugins break too much) With isort - now isort isn't found even though is exists clearly in PATH (but not in virtualenv). With black, tried workaroun... — committed to tony/vim-config-framework by tony 4 years ago
- Remove syntastic. Black on stable Syntastic fights with CoC. Black main is unstable, switch to stable branch for updates. per https://github.com/psf/black/issues/1293 — committed to wcpannell/dotfiles by wcpannell 4 years ago
- Fix Black integration with Vim See https://github.com/psf/black/issues/1293 — committed to 0mp/dotfiles by 0mp 4 years ago
- Downgrade black This is the only version that will work for me until pip 20 and python 3.9: https://github.com/psf/black/issues/1293#issuecomment-734044154 — committed to chiphogg/dotfiles by chiphogg 4 years ago
- Downgrade black because v19.01b has a bug in it. Assholes https://github.com/psf/black/issues/1293 Should work fine once we ditch elasticsearch-curator and can use a more recent version of black — committed to seddy/scripts by seddy 3 years ago
- Freeze black version until https://github.com/psf/black/issues/1293 is resolved — committed to carno/dotfiles by carno 4 years ago
Here’s a workaround using
vim-plugthat should be a bit easier to manage:for a temp local solution this works for me:
or to checkout to the version tag:
git checkout 19.10b0instead.It looks like this function was added in #1273, so the Vim plugin was updated to call this function, while the installed package doesn’t (yet) have this function. Looks like this comment holds the current solution:
I’m using
vim-plugso I did acd ~/.vim/plugged/black. But this workaround works for me. Thanks!With
vim-plug, you can even use a wildcard tag, to avoid having to specify a particular tagged version and updating your init file when a newblackversion is released:Hi, I have a similar situation. I’m also getting errors about pyproject_toml with
stablebranch of Black. Specifying{ 'tag': '19.10b0' }resolves the issue.Hi, I upgraded all the vim-plug plugins recently. And it seems
blackwas broken with the same error. I had to switch to that tag 19.Is this happening with recent release too?
I ran into problems when Vim was upgraded to 8.2.587.
My temporary workaround: I have the vim plugin installed with vim-plug, make sure it’s using master branch (since the released black/vim-black aren’t compatible with latest vim):
Update vim-plugin:
Upgrade black used by the vim-plugin to the latest from master:
This should work when there is a new release of Black on PyPI I reckon.
Guys, please just update your pip version to 20 and python3 to 3.9 then that problem will be solved. At least you can use the latest version of black on your vim.
@erikr That’s weird, but I can confirm this happens with Homebrew Vim 8.2.654 on macOS (I normally use Neovim). It seems to be a completely unrelated problem though, so please file a different issue for that. Feel free to re-use any information provided in the Details section below 😃 (I made it collapsible because I’d rather not hijack this thread any more than it already has been, the issue mentioned in OP has been addressed and this issue should be closed.)
Details
When the virtualenv is created,
vimgets somehow substituted instead ofpython3as the executable name whenensurepipis run:On that last line, it should be
'/Users/david/.vim/black/bin/python3', not'/Users/david/.vim/black/bin/vim'. So ensurepip fails and the installation of the black Python package aborts. Not sure why.If you can’t see that error message, wipe the black virtualenv and restart Vim. If it flies by too quickly, you can redisplay it with
:messages.@erikr Try
au FileType python nnoremap <buffer> <F9> :silent !black -S %<CR><CR>. First<CR>to execute command, second<CR>to press Enter for you automatically.I added this key mapping
Alt + fto use externalblackcommand in$PATH:No more black plugin!
I had the same issue and could solve it with the proposed fix. However, I observed that it only occurred in the python environments in which pynvim was installed.