Skip to content

Commit 4bb6e4f

Browse files
authored
Vim plugin: allow using system black rather than virtualenv (psf#3309)
Provide a configuration parameter to the Vim plugin which will allow the plugin to skip setting up a virtualenv. This is useful when there is a system installation of black (e.g. from a Linux distribution) which the user prefers to use. Using a virtualenv remains the default. - Fixes psf#3308
1 parent d338de7 commit 4bb6e4f

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050

5151
<!-- For example, Docker, GitHub Actions, pre-commit, editors -->
5252

53+
- Vim plugin: Optionally allow using the system installation of Black via
54+
`let g:black_use_virtualenv = 0`(#3309)
55+
5356
### Documentation
5457

5558
<!-- Major changes to documentation and policies. Small docs changes

autoload/black.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ def _get_virtualenv_site_packages(venv_path, pyver):
5656
return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
5757

5858
def _initialize_black_env(upgrade=False):
59+
if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false":
60+
if upgrade:
61+
print("Upgrade disabled due to g:black_use_virtualenv being disabled.")
62+
print("Either use your system package manager (or pip) to upgrade black separately,")
63+
print("or modify your vimrc to have 'let g:black_use_virtualenv = 1'.")
64+
return False
65+
else:
66+
# Nothing needed to be done.
67+
return True
68+
5969
pyver = sys.version_info[:3]
6070
if pyver < (3, 7):
6171
print("Sorry, Black requires Python 3.7+ to run.")

docs/integrations/editors.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Commands and shortcuts:
104104
- you can optionally pass `target_version=<version>` with the same values as in the
105105
command line.
106106
- `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
107-
- `:BlackVersion` to get the current version of _Black_ inside the virtualenv.
107+
- `:BlackVersion` to get the current version of _Black_ in use.
108108

109109
Configuration:
110110

@@ -160,6 +160,18 @@ If you need to do anything special to make your virtualenv work and install _Bla
160160
example you want to run a version from main), create a virtualenv manually and point
161161
`g:black_virtualenv` to it. The plugin will use it.
162162

163+
If you would prefer to use the system installation of _Black_ rather than a virtualenv,
164+
then add this to your vimrc:
165+
166+
```
167+
let g:black_use_virtualenv = 0
168+
```
169+
170+
Note that the `:BlackUpgrade` command is only usable and useful with a virtualenv, so
171+
when the virtualenv is not in use, `:BlackUpgrade` is disabled. If you need to upgrade
172+
the system installation of _Black_, then use your system package manager or pip--
173+
whatever tool you used to install _Black_ originally.
174+
163175
To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`:
164176

165177
```

plugin/black.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ endif
6363
if !exists("g:black_target_version")
6464
let g:black_target_version = ""
6565
endif
66+
if !exists("g:black_use_virtualenv")
67+
let g:black_use_virtualenv = 1
68+
endif
6669
if !exists("g:black_preview")
6770
let g:black_preview = 0
6871
endif

0 commit comments

Comments
 (0)