Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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 (#4675)
- Vim: Fix handling of virtualenvs that have a different Python version (#4675)

### Documentation

Expand Down
16 changes: 14 additions & 2 deletions autoload/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -120,7 +129,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):
Expand Down
Loading