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
-
Install
vim-plug(link). -
Add
Plug 'psf/black'next to the other plugins in~/.vimrc. -
Install via
vimfollowed by:PlugInstall, or viavim -c PlugInstall:
-
Run
vimfrom 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-pluginstallation 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
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
- Add black vim plugin To help format python files. Note some fiddling is required to install this correctly because > This is the error resulting from incompatible versions of black.vim > (the Vim p... — committed to mocoso/dotfiles by mocoso 5 years ago
rm -rf ~/.vim/blackwas 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) andblack.py(the Python library) we were previously discussing in #1293 – you have themasterversion ofblack.vim, which however installs the latest release (i.e. code that’s older thanmaster) ofblack.py. Since the latest release ofblack.vimdoesn’t work with recent versions of Vim, your only option is to updateblack.py(so that bothblack.vimandblack.pyare the same bleeding edge –master– version). As per the instructions you provided in the OP:Thanks, progress!
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:
I thought this error was addressed by #1273. Suggested next move?
This is the error with which the Black virtualenv setup fails:
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:
Now the venv gets recreated if I
:PlugUpdateor I can call:PlugUpdate! blackto force it. It’s somewhat of a hack;:BlackUpgrade(re-)creates the environment if it doesn’t exist and we force that situation with anrm.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.
blackruns fine itself.I identified the problem is with
pyproject.tomland 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.