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:

  1. Open up some python file, writing something, and leave insert mode
  2. Type :Black and hit Enter to save
  3. 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

Most upvoted comments

Here’s a workaround using vim-plug that should be a bit easier to manage:

Plug 'psf/black', { 'tag': '19.10b0' }

for a temp local solution this works for me:

cd ~/.vim/bundle/black/
git checkout ce14fa8b497bae2b50ec48b3bd7022573a59cdb1

or to checkout to the version tag: git checkout 19.10b0 instead.

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:

You need to install black from sources instead of from PyPI, or downgrade the plugin to the last release. To upgrade black:

source ~/.vim/black/bin/activate  # make sure to install in the right venv
pip install --upgrade git+https://github.com/psf/black.git

Would be nice if these kinds of breaks could be avoided in the future, although I am not sure how. Problem is that Plug or Vundle pulls the black vim plugin from current master but black gets installed only for releases on PyPI. Perhaps you should point Vundle or whatever at a tagged release, if possible.

for a temp local solution this works for me:

cd ~/.vim/bundle/black/
git checkout ce14fa8b497bae2b50ec48b3bd7022573a59cdb1

or to checkout to the version tag: git checkout 19.10b0 instead.

I’m using vim-plug so I did a cd ~/.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 new black version is released:

Plug 'psf/black', { 'tag': '*' }

Hi, I have a similar situation. I’m also getting errors about pyproject_toml with stable branch of Black. Specifying { 'tag': '19.10b0' } resolves the issue.

Here’s a workaround using vim-plug that should be a bit easier to manage:

Plug 'psf/black', { 'tag': '19.10b0' }

Hi, I upgraded all the vim-plug plugins recently. And it seems black was 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):

Plug 'psf/black'

Update vim-plugin:

:PlugUpdate black

Upgrade black used by the vim-plugin to the latest from master:

cd ~/.vim
black/bin/pip install --upgrade git+https://github.com/psf/black.git

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, vim gets somehow substituted instead of python3 as the executable name when ensurepip is run:

Please wait, one time setup for Black.
Creating a virtualenv in /Users/david/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Error detected while processing /Users/david/.vim/plugged/black/plugin/black.vim:
line  195:
Traceback (most recent call last):
  File "<string>", line 87, in <module>
  File "<string>", line 70, in _initialize_black_env
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/venv/__init__.py", line 391, in create
    builder.create(env_dir)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/venv/__init__.py", line 68, in create
    self._setup_pip(context)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/venv/__init__.py", line 289, in _setup_pip
    subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/Users/david/.vim/black/bin/vim', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

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 + f to use external black command in $PATH:

au FileType python nnoremap <buffer> <a-f> :silent !black -S %<CR><CR>

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.