Skip to content

Commit 9e1eeb5

Browse files
Fix #1492: Add PIPX_GLOBAL_(HOME|BIN_DIR|MAN_DIR) documentation and list them in pipx environment (#1493)
* List PIPX_GLOBAL_* vars in 'pipx environment' * Add documentation for PIPX_GLOBAL_* vars * Test presence of PIPX_GLOBAL_* in pipx environment * Parameterize list of environment variables * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3b6941b commit 9e1eeb5

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

changelog.d/1492.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
List `PIPX_GLOBAL_[HOME|BIN_DIR|MAN_DIR]` in `pipx environment`.

docs/installation.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,27 @@ Example configuration for use of the code linter [yapf](https://github.com/googl
148148
149149
The default binary location for pipx-installed apps is `~/.local/bin`. This can be overridden with the environment
150150
variable `PIPX_BIN_DIR`. The default manual page location for pipx-installed apps is `~/.local/share/man`. This can be
151-
overridden with the environment variable `PIPX_MAN_DIR`.
151+
overridden with the environment variable `PIPX_MAN_DIR`. If the `--global` option is used, the default locations are
152+
`/usr/local/bin` and `/usr/local/share/man` respectively and can be overridden with `PIPX_GLOBAL_BIN_DIR` and
153+
`PIPX_GLOBAL_MAN_DIR`.
152154

153155
pipx's default virtual environment location is typically `~/.local/share/pipx` on Linux/Unix, `~/.local/pipx` on MacOS
154156
and `~\pipx` on Windows. For compatibility reasons, if `~/.local/pipx` on Linux, `%USERPROFILE%\AppData\Local\pipx` or
155157
`~\.local\pipx` on Windows or `~/Library/Application Support/pipx` on MacOS exists, it will be used as the default location instead.
156-
This can be overridden with the `PIPX_HOME` environment variable.
158+
This can be overridden with the `PIPX_HOME` environment variable. If the `--global` option is used, the default location is always
159+
`/opt/pipx` and can be overridden with `PIPX_GLOBAL_HOME`.
157160

158161
In case one of these fallback locations exist, we recommend either manually moving the pipx files to the new default location
159162
(see the [Moving your pipx installation](installation.md#moving-your-pipx-installation) section of the docs), or setting the
160163
`PIPX_HOME` environment variable (discarding files existing in the fallback location).
161164

162-
As an example, you can install global apps accessible by all users on your system with the following command (on MacOS,
165+
As an example, you can install global apps accessible by all users on your system with either of the following commands (on MacOS,
163166
Linux, and Windows WSL):
164167

165168
```
166-
sudo PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin PIPX_MAN_DIR=/usr/local/share/man pipx install PACKAGE
167-
# Example: $ sudo PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin PIPX_MAN_DIR=/usr/local/share/man pipx install cowsay
169+
sudo PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin PIPX_MAN_DIR=/usr/local/share/man pipx install <PACKAGE>
170+
# or shorter (with pipx>=1.5.0)
171+
sudo pipx install --global <PACKAGE>
168172
```
169173

170174
> [!NOTE]

src/pipx/commands/environment.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
from pipx.interpreter import DEFAULT_PYTHON
77
from pipx.util import PipxError
88

9+
ENVIRONMENT_VARIABLES = [
10+
"PIPX_HOME",
11+
"PIPX_GLOBAL_HOME",
12+
"PIPX_BIN_DIR",
13+
"PIPX_GLOBAL_BIN_DIR",
14+
"PIPX_MAN_DIR",
15+
"PIPX_GLOBAL_MAN_DIR",
16+
"PIPX_SHARED_LIBS",
17+
"PIPX_DEFAULT_PYTHON",
18+
"PIPX_FETCH_MISSING_PYTHON",
19+
"USE_EMOJI",
20+
"PIPX_HOME_ALLOW_SPACE",
21+
]
22+
923

1024
def environment(value: str) -> ExitCode:
1125
"""Print a list of environment variables and paths used by pipx"""
12-
environment_variables = [
13-
"PIPX_HOME",
14-
"PIPX_BIN_DIR",
15-
"PIPX_MAN_DIR",
16-
"PIPX_SHARED_LIBS",
17-
"PIPX_DEFAULT_PYTHON",
18-
"PIPX_FETCH_MISSING_PYTHON",
19-
"USE_EMOJI",
20-
"PIPX_HOME_ALLOW_SPACE",
21-
]
2226
derived_values = {
2327
"PIPX_HOME": paths.ctx.home,
2428
"PIPX_BIN_DIR": paths.ctx.bin_dir,
@@ -36,7 +40,7 @@ def environment(value: str) -> ExitCode:
3640
if value is None:
3741
print("Environment variables (set by user):")
3842
print("")
39-
for env_variable in environment_variables:
43+
for env_variable in ENVIRONMENT_VARIABLES:
4044
env_value = os.getenv(env_variable, "")
4145
print(f"{env_variable}={env_value}")
4246
print("")

src/pipx/main.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pipx import commands, constants, paths
2323
from pipx.animate import hide_cursor, show_cursor
2424
from pipx.colors import bold, green
25+
from pipx.commands.environment import ENVIRONMENT_VARIABLES
2526
from pipx.constants import (
2627
EXIT_CODE_OK,
2728
EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND,
@@ -85,8 +86,11 @@ def prog_name() -> str:
8586
"""
8687
optional environment variables:
8788
PIPX_HOME Overrides default pipx location. Virtual Environments will be installed to $PIPX_HOME/venvs.
89+
PIPX_GLOBAL_HOME Used instead of PIPX_HOME when the `--global` option is given.
8890
PIPX_BIN_DIR Overrides location of app installations. Apps are symlinked or copied here.
91+
PIPX_GLOBAL_BIN_DIR Used instead of PIPX_BIN_DIR when the `--global` option is given.
8992
PIPX_MAN_DIR Overrides location of manual pages installations. Manual pages are symlinked or copied here.
93+
PIPX_GLOBAL_MAN_DIR Used instead of PIPX_MAN_DIR when the `--global` option is given.
9094
PIPX_DEFAULT_PYTHON Overrides default python used for commands.
9195
USE_EMOJI Overrides emoji behavior. Default value varies based on platform.
9296
PIPX_HOME_ALLOW_SPACE Overrides default warning on spaces in the home path
@@ -122,15 +126,24 @@ def prog_name() -> str:
122126
123127
The PACKAGE_SPEC argument is passed directly to `pip install`.
124128
125-
The default virtual environment location is {paths.DEFAULT_PIPX_HOME}
126-
and can be overridden by setting the environment variable `PIPX_HOME`
127-
(Virtual Environments will be installed to `$PIPX_HOME/venvs`).
129+
Virtual Environments will be installed to `$PIPX_HOME/venvs`.
130+
The default pipx home location is {paths.DEFAULT_PIPX_HOME} and can
131+
be overridden by setting the environment variable `PIPX_HOME`.
132+
If the `--global` option is used, the default pipx home location
133+
instead is {paths.DEFAULT_PIPX_GLOBAL_HOME} and can be overridden
134+
by setting the environment variable `PIPX_GLOBAL_HOME`.
128135
129136
The default app location is {paths.DEFAULT_PIPX_BIN_DIR} and can be
130137
overridden by setting the environment variable `PIPX_BIN_DIR`.
138+
If the `--global` option is used, the default app location instead
139+
is {paths.DEFAULT_PIPX_GLOBAL_BIN_DIR} and can be overridden by
140+
setting the environment variable `PIPX_GLOBAL_BIN_DIR`.
131141
132142
The default manual pages location is {paths.DEFAULT_PIPX_MAN_DIR} and
133143
can be overridden by setting the environment variable `PIPX_MAN_DIR`.
144+
If the `--global` option is used, the default manual pages location
145+
instead is {paths.DEFAULT_PIPX_GLOBAL_MAN_DIR} and can be overridden
146+
by setting the environment variable `PIPX_GLOBAL_MAN_DIR`.
134147
135148
The default python executable used to install a package is
136149
{DOC_DEFAULT_PYTHON} and can be overridden
@@ -907,10 +920,9 @@ def _add_environment(subparsers: argparse._SubParsersAction, shared_parser: argp
907920
variables and platform specific default values.
908921
909922
Available variables:
910-
PIPX_HOME, PIPX_BIN_DIR, PIPX_MAN_DIR, PIPX_SHARED_LIBS, PIPX_LOCAL_VENVS,
911-
PIPX_LOG_DIR, PIPX_TRASH_DIR, PIPX_VENV_CACHEDIR, PIPX_DEFAULT_PYTHON, USE_EMOJI, PIPX_HOME_ALLOW_SPACE
912923
"""
913-
),
924+
)
925+
+ textwrap.fill(", ".join(ENVIRONMENT_VARIABLES), break_long_words=False),
914926
parents=[shared_parser],
915927
)
916928
p.add_argument("--value", "-V", metavar="VARIABLE", help="Print the value of the variable.")

tests/test_environment.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from helpers import run_pipx_cli, skip_if_windows
55
from pipx import paths
6+
from pipx.commands.environment import ENVIRONMENT_VARIABLES
67
from pipx.paths import get_expanded_environ
78

89

@@ -18,10 +19,8 @@ def test_cli(pipx_temp_env, monkeypatch, capsys):
1819
assert fnmatch.fnmatch(captured.out, "*PIPX_TRASH_DIR=*subdir/pipxhome/.trash*")
1920
assert fnmatch.fnmatch(captured.out, "*PIPX_VENV_CACHEDIR=*subdir/pipxhome/.cache*")
2021
# Checking just for the sake of completeness
21-
assert "PIPX_DEFAULT_PYTHON" in captured.out
22-
assert "USE_EMOJI" in captured.out
23-
assert "PIPX_HOME_ALLOW_SPACE" in captured.out
24-
assert "Environment variables (set by user):" in captured.out
22+
for env_var in ENVIRONMENT_VARIABLES:
23+
assert env_var in captured.out
2524

2625

2726
def test_cli_with_args(monkeypatch, capsys):
@@ -88,6 +87,5 @@ def test_cli_global(pipx_temp_env, monkeypatch, capsys):
8887
assert fnmatch.fnmatch(captured.out, "*PIPX_TRASH_DIR=*global/pipxhome/.trash*")
8988
assert fnmatch.fnmatch(captured.out, "*PIPX_VENV_CACHEDIR=*global/pipxhome/.cache*")
9089
# Checking just for the sake of completeness
91-
assert "PIPX_DEFAULT_PYTHON" in captured.out
92-
assert "USE_EMOJI" in captured.out
93-
assert "PIPX_DEFAULT_PYTHON" in captured.out
90+
for env_var in ENVIRONMENT_VARIABLES:
91+
assert env_var in captured.out

0 commit comments

Comments
 (0)