Black: Unable to use black inside of vim: Ubuntu 14, python 3.6, vim version 8.1.133

Created on 2 Jul 2018  路  9Comments  路  Source: psf/black

Operating system:

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty

Python version:

Python 3.6.3

Black version:

black, version 18.6b4

Does also happen on master: [yes]

The command black runs from the terminal.

I have installed the vim plugin using Vundle.

When I launch vim I get the following:

Error detected while processing [snip]/vim-config/dotvim/bundle/black/plugin/black.vim:
line  120:
  File "<string>", line 17
    return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
                                                            ^
SyntaxError: invalid syntax

Here is my vim information:

version 8.1.133
+python3

[I followed the steps below to see if it would make a difference.]
To answer the last question, follow these steps:

  • create a new virtualenv (make sure it's the same Python version);
  • clone this repository;
  • run pip install -e .;
  • make sure it's sane by running python setup.py test; and
  • run black like you did last time.

Thanks for your time. I look forward to trying Black.

Most helpful comment

In my opinion this is too much to ask from a user to recompile vim, just to run black. I have chosen instead to have black installed in my conda environment, and have a simple line in the ~/.vimrc

command Black !black -l 82 %:p

where 82 is the line length my project uses. Just :Black would reformat my current file and prompt me to load it :)

All 9 comments

This is usually because your vim is compiled with python <3.6

easiest way to tell is by doing something like this:

$ ldd $(which vim) | grep python
libpython3.4m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0 (0x00007f5ad1ad0000)

Mine is compiled with 3.4 so I get the same error ;)

I discovered that I had a typo in my ./configure flags so vim was indeed compiled with python3.4 instead of what I thought was python3.6. Silly mistake. Thanks for the help. All is well again.

How do I compile it with a different Python?

In my opinion this is too much to ask from a user to recompile vim, just to run black. I have chosen instead to have black installed in my conda environment, and have a simple line in the ~/.vimrc

command Black !black -l 82 %:p

where 82 is the line length my project uses. Just :Black would reformat my current file and prompt me to load it :)

After my last comment I discovered how to recompile vim with a different Python and was about to post that here, but forgot about it and now I don't know how to recompile vim again.

@fiatjaf it is indeed cumbersome. Another alternative is to use neovim (easily deployable as an AppImage!) which lets you switch the Python compiler simply by configuring it. For instance I have a ~/.config/nvim/init.vim which looks like this:

let g:python3_host_prog = '~/miniconda3/envs/vim/bin/python'
execute('source ~/.vimrc')

I appreciate your NeoVim pitch and I'm in the process of experimenting NeoVim to maybe migrate to it, but about AppImages I have to disagree: I've tried to run AppImages in many occasions and they never work for me.

I know the issue has been closed. I'm using debian 9.9 and the vim version shipped by debian is linked against py35. I certainly don't want to my self-compiled version of vim just for using an extension. So, what should we debian user do?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lukas0907 picture Lukas0907  路  66Comments

mhham picture mhham  路  22Comments

Kristinita picture Kristinita  路  28Comments

odormond picture odormond  路  21Comments

ambv picture ambv  路  51Comments