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
Return site-packages for the venv Python version
  • Loading branch information
sbivol committed Feb 19, 2024
commit f6bdef41cdeec22065259d684f73bee05fb6892f
13 changes: 10 additions & 3 deletions autoload/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,22 @@ def _get_python_binary(exec_prefix, pyver=sys.version_info[:3]):
return exec_path
raise ValueError("python executable not found")

def _get_python_version(exec_path):
import subprocess
version_proc = subprocess.run([exec_path, "--version"], stdout=subprocess.PIPE, text=True)
version = tuple(map(int, version_proc.stdout.split(" ")[1].strip().split(".")))
return version

def _get_pip(venv_path):
if sys.platform[:3] == "win":
return venv_path / 'Scripts' / 'pip.exe'
return venv_path / 'bin' / 'pip'

def _get_virtualenv_site_packages(venv_path, pyver):
def _get_virtualenv_site_packages(venv_path):
if sys.platform[:3] == "win":
return venv_path / 'Lib' / 'site-packages'
return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
venv_python_version = _get_python_version(_get_python_binary(venv_path))
return venv_path / 'lib' / f'python{venv_python_version[0]}.{venv_python_version[1]}' / 'site-packages'

def _initialize_black_env(upgrade=False):
if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false":
Expand All @@ -83,7 +90,7 @@ def _initialize_black_env(upgrade=False):
import subprocess
import venv
virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver))
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