Skip to content

Conversation

@dlukes
Copy link
Contributor

@dlukes dlukes commented May 4, 2020

Resolves #1379.

At least in Python 3.8, the venv module relies on sys._base_executable to determine the Python executable to run, but with recent versions of Vim (8.2.654 from Homebrew on macOS), this attribute is set to the Vim executable instead of Python: running :python3 import sys; print(sys._base_executable) prints /usr/local/bin/vim. This makes the Black Vim plugin one-time setup (i.e. virtualenv creation and Black Python package installation) fail at the ensurepip stage (because vim -Im ensurepip ... doesn't make any sense).

A possible workaround is to just override the sys._base_executable attribute, especially since the Black plugin already overrides sys.executable (possibly for similar reasons?) anyway. This is what the present patch does.

More generally though, is this intended behavior or a bug in recent versions of Python and/or Vim? I'm leaning towards a bug in Vim, since running :python3 import sys; print(sys._base_executable) in Neovim correctly prints a Python path, even with Python 3.8. Just trying to figure out whether this should be reported somewhere else as well :)

The `venv` module relies on `sys._base_executable` to determine the
Python executable to run, but with recent versions of Vim, this is set
to the `vim` executable. A possible workaround is to just override it,
since the `black` plugin already overrides `sys.executable` (possibly
for similar reasons?) anyway.
@dlukes
Copy link
Contributor Author

dlukes commented May 4, 2020

OK, did a bit more digging and:

  • Both sys.executable and sys._base_executable are wrong in Vim on macOS.
  • It's a known issue on Vim's side.

So my conclusion is that the sys.executable = ... hack which was already there previously in black.vim was added precisely for this reason, and just stopped working because venv in Python 3.8 started relying on sys._base_executable instead. So extending the hack to sys._base_executable should hopefully be uncontroversial :)

@dlukes
Copy link
Contributor Author

dlukes commented May 4, 2020

Giving it some more thought, I think it's also a good idea to wipe the virtualenv when virtualenv creation fails part way through (cf. added except ... clause). This makes sure the one-time setup will be re-attempted when Vim is restarted, making it more likely the user will actually notice the relevant errors (instead of just experiencing No module named 'black'). And hopefully, users will less often need to wipe the virtualenv by hand.

@dlukes dlukes changed the title Make sure sys._base_executable is sane Make sure sys._base_executable is sane in Vim plugin May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Installed black via vim-plug on macOS; starting vim throws ModuleNotFoundError: No module named 'black'

2 participants