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

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Prev Previous commit
Next Next commit
Add site-packages to sys.path when black is in a non-default virtualenv
  • Loading branch information
sbivol committed Feb 20, 2024
commit 273ea5979666a09f52b0528f72dbc96fe25cb2a8
10 changes: 6 additions & 4 deletions autoload/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import os
import sys
import vim

from pathlib import Path

def strtobool(text):
if text.lower() in ['y', 'yes', 't', 'true', 'on', '1']:
return True
Expand Down Expand Up @@ -71,26 +73,26 @@ def _get_virtualenv_site_packages(venv_path):
return venv_path / 'lib' / f'python{venv_python_version[0]}.{venv_python_version[1]}' / 'site-packages'

def _initialize_black_env(upgrade=False):
virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path))
if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false":
if upgrade:
print("Upgrade disabled due to g:black_use_virtualenv being disabled.")
print("Either use your system package manager (or pip) to upgrade black separately,")
print("or modify your vimrc to have 'let g:black_use_virtualenv = 1'.")
return False
else:
# Nothing needed to be done.
if virtualenv_site_packages not in sys.path:
sys.path.insert(0, virtualenv_site_packages)
return True

pyver = sys.version_info[:3]
if pyver < (3, 8):
print("Sorry, Black requires Python 3.8+ to run.")
return False

from pathlib import Path
import subprocess
import venv
virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path))
first_install = False
if not virtualenv_path.is_dir():
print('Please wait, one time setup for Black.')
Expand Down