Skip to content

[WIP] Allow Black to use a different Python version than Vim#4245

Closed
sbivol wants to merge 8 commits into
psf:mainfrom
sbivol:fix/different-venv-python
Closed

[WIP] Allow Black to use a different Python version than Vim#4245
sbivol wants to merge 8 commits into
psf:mainfrom
sbivol:fix/different-venv-python

Conversation

@sbivol
Copy link
Copy Markdown
Contributor

@sbivol sbivol commented Feb 20, 2024

Description

This branch should fix #2547

It is sometimes the case that Vim was built with a different Python version than the one used by Black.
One such scenario is when the black virtualenv was created by an older version of Vim and then the system was upgraded in place.
Another scenario is when g:black_use_virtualenv is disabled and the manually created environment in g:black_virtualenv uses a different Python version. Right now Ubuntu 24.04 builds Vim with Python 3.12 but the system uses Python 3.11 as default, making this scenario more likely to happen:

$ python3 --version
Python 3.11.8

$ vim --version | grep lpython
Linking: gcc -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lm -ltinfo -lselinux -lsodium -lacl -lattr -lgpm -L/usr/lib/python3.12/config-3.12-x86_64-linux-gnu -lpython3.12 -ldl -lm

Currently, :BlackVersion reports Vim's Python version instead of the one found in the virtualenv. This PR fixes that inconsistency as well.

Finally, I added a few TODO comments for issues that I encountered while testing various scenarios but didn't have the time to fix in this PR.

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Copy link
Copy Markdown
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment thread autoload/black.vim
sys.path.insert(0, virtualenv_site_packages)
return True

# TODO: Is this check about the Vim plugin, or about Black?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about Black primarily; Black itself requires 3.8+ (since Python 3.7 is no longer supported). By default I would want the same version requirement for the Vim plugin, but if there's some reason to keep supporting older versions in the plugin code, I'm open to it.

Comment thread autoload/black.vim

if _initialize_black_env():
import black
# TODO: Better handling of the case when import succeeds but Black doesn't
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment; if Black is uninstalled, surely the import would fail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, not always. This can be triggered by Vim built with Py3.12 importing black from env/lib/python3.11/site-packages, which this PR makes possible.
If black is uninstalled after it was used once in such a setup, then pip uninstall leaves site-packages/black/__pycache__/ behind and that causes import black to succeed:

$ env/bin/python3 -c "import black; print(dir(black)); print(black.__file__)"
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
None

It's a corner case of a corner case, but it's a pain to troubleshoot when it bites.

@sbivol
Copy link
Copy Markdown
Contributor Author

sbivol commented Feb 20, 2024

An alternative approach is to detect the Python version in the virtualenv, and if it doesn't match what Vim was built with, then go through these steps:

  • inform the user that Vim won't use that virtualenv
  • reset g:black_virtualenv to its default value
  • create the default virtualenv as usual

This approach wouldn't enable mixing different Python versions and would use a bit of extra space for a new ~/.vim/black virtualenv, but should still leave the user with a working Black and no chance of confusion.

@sbivol sbivol changed the title Allow Black to use a different Python version than Vim [WIP] Allow Black to use a different Python version than Vim Feb 20, 2024
@sbivol sbivol marked this pull request as draft February 20, 2024 21:14
@sbivol
Copy link
Copy Markdown
Contributor Author

sbivol commented Feb 20, 2024

After giving this some more thought, and further discussion in pypa/pip#11835, I'm now convinced that my approach isn't ideal. Mixing Python versions can have undesired side effects.
I am going to rework this PR.

@sbivol
Copy link
Copy Markdown
Contributor Author

sbivol commented May 31, 2025

I used a different approach in #4675 which should hopefully make the changes easier to review and to test.
Closing this PR.

@sbivol sbivol closed this May 31, 2025
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.

Plugin can't find black python module

2 participants