From d6e9954bb59edb3aabf0d8c83a6a092ee6282607 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 21:28:03 +0000 Subject: [PATCH 1/4] Print the import paths when importing black fails --- CHANGES.md | 1 + autoload/black.vim | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ca6fe3f653a..fcadf858371 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -67,6 +67,7 @@ - Enhance GitHub Action `psf/black` to read Black version from an additional section in pyproject.toml: `[project.dependency-groups]` (#4606) - Build gallery docker image with python3-slim and reduce image size (#4686) +- Vim: Print the import paths when importing black fails ### Documentation diff --git a/autoload/black.vim b/autoload/black.vim index cb2233c465a..60560482b91 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -120,7 +120,10 @@ def _initialize_black_env(upgrade=False): return True if _initialize_black_env(): - import black + try: + import black + except ImportError: + print(f"Could not import black from any of: {', '.join(sys.path)}.") import time def get_target_version(tv): From 8dac8376c0f3a62f152ac405d7cd68c26f873fe2 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Sat, 31 May 2025 02:06:04 +0100 Subject: [PATCH 2/4] Fix handling of virtualenvs that have a different Python version This commit only addresses the `g:black_use_virtualenv = 1` case (which is the default). --- CHANGES.md | 1 + autoload/black.vim | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index fcadf858371..e1ca5db2892 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -68,6 +68,7 @@ pyproject.toml: `[project.dependency-groups]` (#4606) - Build gallery docker image with python3-slim and reduce image size (#4686) - Vim: Print the import paths when importing black fails +- Vim: Fix handling of virtualenvs that have a different Python version ### Documentation diff --git a/autoload/black.vim b/autoload/black.vim index 60560482b91..15c5992ee3d 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -61,7 +61,16 @@ def _get_pip(venv_path): def _get_virtualenv_site_packages(venv_path, pyver): if sys.platform[:3] == "win": return venv_path / 'Lib' / 'site-packages' - return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages' + if venv_path.exists() and not (venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}').exists(): + # The virtualenv already exists but it doesn't seem to have the expected + # Python version, so we disregard the requested `pyver` and + # discover the real Python interpreter from this virtualenv + import subprocess + result = subprocess.run([_get_python_binary(venv_path, pyver), "--version"], stdout=subprocess.PIPE, text=True) + venv_version = result.stdout.split(" ")[1].strip().split(".") + return venv_path / 'lib' / f'python{venv_version[0]}.{venv_version[1]}' / 'site-packages' + else: + return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages' def _initialize_black_env(upgrade=False): if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false": From 2229e733e52863073bac786f2b4ebaed7ecd87cc Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Sun, 1 Jun 2025 14:59:04 +0100 Subject: [PATCH 3/4] Update CHANGES.md Co-authored-by: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e1ca5db2892..bacb8809302 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -67,8 +67,8 @@ - Enhance GitHub Action `psf/black` to read Black version from an additional section in pyproject.toml: `[project.dependency-groups]` (#4606) - Build gallery docker image with python3-slim and reduce image size (#4686) -- Vim: Print the import paths when importing black fails -- Vim: Fix handling of virtualenvs that have a different Python version +- Vim: Print the import paths when importing black fails (#4675) +- Vim: Fix handling of virtualenvs that have a different Python version (#4675) ### Documentation From 439d7b9708e8b2fdfef838c2654f3f2e0c452a91 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Sat, 4 Oct 2025 11:16:24 -0700 Subject: [PATCH 4/4] Move changes entry to new unreleased section --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1efddd98156..98e4f6506ad 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -47,6 +47,8 @@ - Enhance GitHub Action `psf/black` to support the `required-version` major-version-only "stability" format when using pyproject.toml (#4770) +- Vim: Print the import paths when importing black fails (#4675) +- Vim: Fix handling of virtualenvs that have a different Python version (#4675) ### Documentation @@ -104,8 +106,6 @@ - Enhance GitHub Action `psf/black` to read Black version from an additional section in pyproject.toml: `[project.dependency-groups]` (#4606) - Build gallery docker image with python3-slim and reduce image size (#4686) -- Vim: Print the import paths when importing black fails (#4675) -- Vim: Fix handling of virtualenvs that have a different Python version (#4675) ### Documentation