Operating system: Ubuntu 18.04
Python version: 3.6.6
Black version: whichever version the vim plugin is trying and failing to install
Does also happen on master: see above
Hey, I installed and used the vim black plugin perfectly fine on macOS, but when I tried it in ubuntu I ran into some problems.
During installation with vim-plug i get the following error: No module named 'black'. Not sure why this is, I did the same exact thing I was doing in macOS, and I am not in any virtualenv or anything weird. The ubuntu machine has python2.7 (under python) and python3.6.6 (under python3) installed. No idea why this could be happening. After this failed installation, each time i try to open vim I get the same error message. Tried installing several times, always get the same error.
Thanks for the awesome project btw!
Edit: The only way I can start vim without getting the error message is if I run it in a virtualenv that has black installed, after this black runs fine. But if I dont do this I get the error mistake.
Hi, I have a similar issue (on Ubuntu). Black works fine in VScode and within virtualenv. Adding black to Vim via Plug or manually. I can't seem to get it working
When running :Black - result below
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'Black' is not defined
opening a *.py file
Error detected while processing ~/.vim/plugged/black/plugin/black.vim:
line 124:
Traceback (most recent call last):
File "<string>", line 56, in <module>
ModuleNotFoundError: No module named 'black'
opening black.vim file
Error detected while processing ~/.vim/plugged/black/plugin/black.vim:
line 124:
Traceback (most recent call last):
File "<string>", line 56, in <module>
File "/home/imbatmam/.vim/plugged/black/black.py", line 39, in <module>
from appdirs import user_cache_dir
ModuleNotFoundError: No module named 'appdirs'
For me issue was fixed after Python3.7 was installed as system python3 interpreter.
I have a workaround that I hope will help anyone else with this error.
Firstly, I am using a similar setup as @joaqo:
Operating system: Ubuntu 18.04
Python version: 3.6.7 (i.e. $ python3 --version prints "Python 3.6.7")
I use Vundle, so my .vimrc has Plugin 'ambv/black'. I also have let g:black_virtualenv="~/.vim_black". The first time I opened vim with the plugin installed, I got the following output:
$ vim black.vim
Please wait, one time setup for Black.
Creating a virtualenv in /home/ronalabraham/.vim_black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
...etc
So I did exactly what the output recommended: sudo apt-get install python3-venv. Re-opening vim then installed Black correctly in the expected directory:
$ vim
Please wait, one time setup for Black.
Creating a virtualenv in /home/ronalabraham/.vim_black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Installing Black with pip...
DONE! You are all set, thanks for waiting ✨ 🍰 ✨
This fixed the ModuleNotFoundError: No module named 'black' error when re-opening vim in the future!
In adition to the super useful comment of @ronalabraham
What worked for me was:
sudo apt-get install python3-venv
python3 -m venv ~/.vim_black
source ~/.vim_black/bin/activate
pip install black
Then inside the .vimrc I added g:black_virtualenv="~/.vim_black"
Seems that if you only g:black_virtualenv="~/.vim_black" and try to install the plugin (at least with vim-plug) it will not properly create the venv with black for you.
I'm not sure if this is a bug, it seems not, but it wasn't clear for me that I should be the one setting the venv, maybe worth some mention about this in the documentation.
Any activities on this issue recently? I'm using Vim 8.1 and hitting the same error. Tried the solutions mentioned above but no success. Still the same error. However, NeoVim does work correctly. One thing I'm extremely confused is the exact order in which I install black, activate the virtualenv, and install the black vim plugin. Any help would be extremely appreciated.
@rollschild try to remove the directory that g:black_virtualenv is referencing and open vim again
This is usually caused by Python and/or Vim upgrading under you while an old virtualenv is still around. The plugin should gracefully say that the virtualenv is unusable. I'd accept a PR to this effect. We have other priorities at the moment.
OK, the simplist fix I've seen on a mac for this is to reinstall black with
rm -f ~/.vim/black
Which causes a new environment to get downloaded. You could also try :BlackUpgrade
Most helpful comment
In adition to the super useful comment of @ronalabraham
What worked for me was:
sudo apt-get install python3-venvpython3 -m venv ~/.vim_blacksource ~/.vim_black/bin/activatepip install blackThen inside the
.vimrcI addedg:black_virtualenv="~/.vim_black"Seems that if you only
g:black_virtualenv="~/.vim_black"and try to install the plugin (at least with vim-plug) it will not properly create the venv with black for you.I'm not sure if this is a bug, it seems not, but it wasn't clear for me that I should be the one setting the venv, maybe worth some mention about this in the documentation.