black: Installed black via vim-plug on macOS; starting vim throws ModuleNotFoundError: No module named 'black'

Describe the bug I get ModuleNotFoundError: No module named 'black' when I run vim, and thus cannot use black in vim on macOS:

$ vim
Error detected while processing /Users/erik/.vim/plugged/black/plugin/black.vim:
line  194:
Traceback (most recent call last):
  File "<string>", line 87, in <module>
ModuleNotFoundError: No module named 'black'
Press ENTER or type command to continue

Surely I am not the only macOS + vim user who wants to use black!

To Reproduce

  1. Install vim-plug (link).

  2. Add Plug 'psf/black' next to the other plugins in ~/.vimrc.

  3. Install via vim followed by :PlugInstall, or via vim -c PlugInstall:

    Screen Shot 2020-05-04 at 4 32 04 PM

  4. Run vim from terminal.

Expected behavior I expect to not get that error, for vim to load, and for me to be able to use black as described in the README.

Environment (please complete the following information):

  • Version: whichever is pulled by the default vim-plug installation instructions. Is it not master?
  • OS and Python version: macOS 10.15.4, Python 3.7.6 managed by Anaconda
  • Vim: 8.2 (installed via Homebrew)

Additional context @akselsjogren suggested upgrading 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

However I do not have pip in bin and thus cannot try this:

$ tree ~/.vim/black
.
├── bin
│   ├── python
│   ├── python3
│   └── vim
├── include
├── lib
│   └── python3.8
└── pyvenv.cfg

Also, see #1293 for further discussion and other things I’ve tried to fix.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

rm -rf ~/.vim/black was literally all I needed - thanks @erikr. Starting Vim again saw the virtualenv created; I was coming from a very old version of the plugin.

This is the error resulting from incompatible versions of black.vim (the Vim plugin) and black.py (the Python library) we were previously discussing in #1293 – you have the master version of black.vim, which however installs the latest release (i.e. code that’s older than master) of black.py. Since the latest release of black.vim doesn’t work with recent versions of Vim, your only option is to update black.py (so that both black.vim and black.py are the same bleeding edge – master – version). As per the instructions you provided in the OP:

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

Thanks, progress!

$ rm -rf ~/.vim/black
$ vim
Please wait, one time setup for Black.
Creating a virtualenv in /Users/erik/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Installing Black with pip...WARNING: You are using pip version 19.2.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Press ENTER or type command to continue

Installation took a long time (2 minutes), but it solved this particular error.

I think your PR #1380 should be linked to this issue to automate closure.

Now there is a new problem:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 94, in Black
  File "<string>", line 130, in get_configs
AttributeError: module 'black' has no attribute 'find_pyproject_toml'

I thought this error was addressed by #1273. Suggested next move?

This is the error with which the Black virtualenv setup fails:

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.

Note the last line, the command starts with '/Users/david/.vim/black/bin/vim' instead of '/Users/david/.vim/black/bin/python3'.

I’ve done a bit of spelunking and I think it’s ultimately a bug in newer versions of Vim. I’ve proposed a workaround in #1380. Can you please try it out and report back whether it was of any help, @erikr?

Success! Thanks for your patience and clear explanations! I was confused about the difference between release and master.

Should I close this issue or wait for your PR to close it?

Had the same issue again after updating Vim via homebrew (Vim is now built with Python 3.8 vs 3.7 previously) causing the the same ModuleNotFoundError error – presumably since the venv no longer functions with a now-missing version of Python. I wrote this for my vim-plug configuration:

function! UpdateBlack(info)
  if a:info.status != 'unchanged' || a:info.force
    silent !rm -rf ~/.vim/black
    redraw!
    BlackUpgrade
  endif
endfunction
Plug 'psf/black', { 'do': function('UpdateBlack') }

Now the venv gets recreated if I :PlugUpdate or I can call :PlugUpdate! black to force it. It’s somewhat of a hack; :BlackUpgrade (re-)creates the environment if it doesn’t exist and we force that situation with an rm.

Also, you can add ‘print(virtualenv_site_packages)’ on line 80 in file ‘.vim/plugged/black/plugin/black.vim’ to see where vim? is looking for the black package and the what version the python should be in the virtual environment.

Try running ~/.vim/black/bin/black on the same file from the command line. If you get the same error, then it’s a bug in black itself, not in its Vim integration.

black runs fine itself.

I identified the problem is with pyproject.toml and opened a new issue https://github.com/psf/black/issues/1496.

Thanks for the prompt and helpful reply!

@erikr Waiting for PR is recommended. Unless this issue is unrelated to that pull request.