Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Black inside Vim does not work #3672

Closed
A-tA-v opened this issue May 1, 2023 · 4 comments
Closed

Black inside Vim does not work #3672

A-tA-v opened this issue May 1, 2023 · 4 comments
Labels
T: bug Something isn't working

Comments

@A-tA-v
Copy link

A-tA-v commented May 1, 2023

Describe the bug

Installed psf/black Vim plugin using Vundle.
Installation went smoothly.
However, I get a bug upon formatting a Python file.
Tried with and without the following line in .vimrc:

let g:black_use_virtualenv = 0

Both variants give the same error.

To Reproduce

From inside Vim editor trying to format the current buffer: 
:Black

The resulting error is:

Error detected while processing /home/my-name/.vim/bundle/black/autoload/black.vim:
line  222:
Traceback (most recent call last):
  File "<string>", line 122, 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

Expected behavior

Expect file formatted.

Environment

  • Black's version:
  • OS and Python version:

Black 23.3.0
Ubuntu 20.04
Python 3.9 (note, default Python is 3.8 but 3.9 is also installable)
Vim is launched in terminal inside my virtual environment.
Black is installed in that virtual environment:

pip3 install black[d]

Currently, I am using the following command in .vimrc:

let g:black_use_virtualenv = 0

assuming that no additional virtual environment is installed by psf plugin,
rather mine environment is being used.

Additional context

@A-tA-v A-tA-v added the T: bug Something isn't working label May 1, 2023
@metov
Copy link

metov commented May 7, 2023

ModuleNotFoundError sounds like it's not using the same env after all. Does vim have a way of listing packages in its env or showing their paths? You could use that to figure out which Python it's using, and then just do /path/to/python -m pip install black to be done with it.

Looking at

black/plugin/black.vim

Lines 35 to 37 in eb32729

let g:black_virtualenv = "~/.local/share/nvim/black"
else
let g:black_virtualenv = "~/.vim/black"
, it sounds like you should find a Python env in ~/.local/share/nvim/black or ~/.vim/black?

@A-tA-v
Copy link
Author

A-tA-v commented May 10, 2023

It turns out the problem happens on Ubuntu, but not on Mac OS (just tried on OSX and it works).

Please wait, one time setup for Black.
Creating a virtualenv in /home/my-name/.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/my-name/.vim/bundle/black/autoload/black.vim:
line  222:
Traceback (most recent call last):
  File "<string>", line 122, 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

@A-tA-v
Copy link
Author

A-tA-v commented May 10, 2023

The psf/black plugin uses a separate virtual environment while my environment already has Black installed.
This makes the installation process more complicated.
The following plugin invokes the default Black as a backend without any issues:
https://github.com/vim-autoformat/vim-autoformat

@hauntsaninja
Copy link
Collaborator

Closing as a duplicate of #2547

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants