black icon indicating copy to clipboard operation
black copied to clipboard

Plugin can't find black python module

Open scottwn opened this issue 4 years ago • 9 comments

Describe the bug I have installed Black using vim-plug. Installation is successful and I can update the plugin no problem. However when I run black inside Vim I get the following error

Error detected while processing /Users/scott/.vim/plugged/black/autoload/black.vim:
line  195:
Traceback (most recent call last):
  File "<string>", line 97, in <module>
ModuleNotFoundError: No module named 'black'
Error detected while processing function black#Black:
line   10:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'Black' is not defined

To Reproduce Install Black via vim-plug Run black in Vim

Expected behavior

I expect Black to run successfully

Environment (please complete the following information):

  • Version: main
  • OS and Python version: Mac OS 11.6 Big Sur, Python 3.10

Does this bug also happen on main? Yes, I've forced vim-plug to use main and we can see that in the PlugUpdate output.

  1 Updated. Elapsed time: 3.993817 sec.
  2 [============]
  3
  4 - Finishing ... Done!
  5 - neoformat: Already up to date.
  6 - MatchTagAlways: Already up to date.
  7 - vim-es2015-snippets: Already up to date.
  8 - vim-react-snippets: Already up to date.
  9 - vim-jsx: Already up to date.
 10 - vim-prettier: Already up to date.
 11 - tsuquyomi: Already up to date.
 12 - typescript-vim: Already up to date.
 13 - vim-snippets: Already up to date.
 14 - ultisnips: Already up to date.
 15 - black: Your branch is up to date with 'origin/main'.
 16 - vim-javascript: Already up to date.

Additional context

I am using Vim inside Fish shell.

scottwn avatar Oct 19 '21 22:10 scottwn

After clearing out the existing installation of Black and reinstalling with vim-plug using 'branch': 'stable' as indicated in the doc, I am getting a regex error.

Please wait, one time setup for Black.WARNING: You are using pip version 21.2.4; however, version 21.3 is available.
               You should consider upgrading via the '/Users/scott/.vim/black/bin/python3 -m pip install --upgrade pip' command.

Creating a virtualenv in /Users/scott/.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 ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Error detected while processing /Users/scott/.vim/plugged/black/autoload/black.vim:
line  161:
Traceback (most recent call last):
  File "<string>", line 97, in <module>
  File "/Users/scott/.vim/black/lib/python3.9/site-packages/black/__init__.py", line 13, in <module>
    import regex as re
  File "/Users/scott/.vim/black/lib/python3.9/site-packages/regex/__init__.py", line 1, in <module>
    from .regex import *
  File "/Users/scott/.vim/black/lib/python3.9/site-packages/regex/regex.py", line 419, in <module>
    import regex._regex_core as _regex_core
  File "/Users/scott/.vim/black/lib/python3.9/site-packages/regex/_regex_core.py", line 21, in <modul
e>
    import regex._regex as _regex
ImportError: dlopen(/Users/scott/.vim/black/lib/python3.9/site-packages/regex/_regex.cpython-39-darwi
n.so, 2): no suitable image found.  Did find:
^I/Users/scott/.vim/black/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so: code signatu
re in (/Users/scott/.vim/black/lib/python3.9/site-packages/regex/_regex.cpython-39-darwin.so) not val
id for use in process using Library Validation: Trying to load an unsigned library
Error detected while processing function black#Black:
line    1:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'Black' is not defined

scottwn avatar Oct 19 '21 22:10 scottwn

Reinstalled regex in the venv and it worked; I will work on a PR to update the doc or see if it can be fixed in vim-plug installation.

scottwn avatar Oct 19 '21 22:10 scottwn

Hi, I had the same issue. In my case, removing the "~/.local/share/nvim/black" folder did the work. However, I'm pointing to main instead of the stable branch due to this issue https://github.com/psf/black/issues/2503

johangp avatar Nov 10 '21 10:11 johangp

Having the same issue, but with Vundle instead of vim-plug. Removing the ~/.vim/black/ folder worked.

AntiCompositeNumber avatar Nov 29 '21 22:11 AntiCompositeNumber

I'm having this issue, but nuking ~/.vim/plugged/black doesn't seem to have any effect.

Contents of my ~/.vimrc:

call plug#begin('~/.vim/plugged')
Plug 'psf/black', { 'branch': 'stable' }
call plug#end

:PlugInstall appears to successfully install black, and re-running it I get:

- black: Already installed

The traceback of the error I'm getting is:

Error detected while processing /home/user/.vim/plugged/black/autoload/black.vim:
line  202:
Traceback (most recent call last):
  File "<string>", line 103, in <module>
ModuleNotFoundError: No module named 'black'
Error detected while processing function black#Black:
line   10:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'Black' is not defined

This happens both when I do and don't have a virtualenv activated. I'm getting this in vim 8.2 on arch linux. Any guidance on how to resolve would be appreciated.

EDIT:

I tried both of the following alternatives as suggested by https://github.com/psf/black/issues/2503, to no effect

Plug 'psf/black', { 'branch': 'main' }
Plug 'psf/black', { 'tag': 'stable' }

samuel-emrys avatar Jan 22 '22 07:01 samuel-emrys

Try removing .vim/black, update your plugins, then run :Black. That will reinstall the virtual environment to match the current system Python version.

AntiCompositeNumber avatar Jan 23 '22 22:01 AntiCompositeNumber

Try removing .vim/black, update your plugins, then run :Black. That will reinstall the virtual environment to match the current system Python version.

Yep this was it, thanks. Didn't see that the virtualenv was there :)

samuel-emrys avatar Jan 23 '22 22:01 samuel-emrys

I have the same bug, but removing and reinstalling does not help.

I did the fallowing to solve the problem:

  1. Removed black from .vim/bundle
  2. Removed black from .vim
  3. Installed Black by PluginInstall
  4. Installed Black by typing :Black

The effect is fallowing:

Please wait, one time setup for Black.
Creating a virtualenv in /home/daniel/.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 ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Error detected while processing /home/daniel/.vim/bundle/black/autoload/black.vim:
line  202:
Traceback (most recent call last):
  File "<string>", line 103, in <module>
ModuleNotFoundError: No module named 'black'
Error detected while processing function black#Black:
line   10:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'Black' is not defined
Press ENTER or type command to continue

DanielWicz avatar Feb 08 '22 10:02 DanielWicz

@DanielWicz I was having the same issue as you, but then I realized that I had a python venv active when I opened vim. Deactivating that venv resolved things for me; maybe it will for you too. Specifically, the venv needs to be deactivated for your step (4).

(some_venv) $ vim
[vim]: PlugInstall
[vim]: :qa
(some_venv) $
(some_venv) $ deactivate
$ vim
[vim]: Black
Please wait, one time setup for Black.
Creating a virtualenv in /usr/local/google/home/dthor/.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 ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Black: already well formatted, good job. (took 0.0034s)
Press ENTER or type command to continue
[vim]: :qa

dougthor42 avatar Jan 08 '25 04:01 dougthor42

There is a tentative fix for this issue in #4675, please test it and report your findings.

sbivol avatar Sep 10 '25 13:09 sbivol