Skip to content

Commit

Permalink
Change the program name to path/to/python -m pipx when running as `…
Browse files Browse the repository at this point in the history
…python -m pipx` (#852)
  • Loading branch information
dukecat0 authored Jun 27, 2022
1 parent 258cb27 commit 243c631
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- [docs] Fix `pipx run` examples and update Python versions used by `pipx install` examples
- [docs] Add an example for installation from source with extras

- Change the program name to `path/to/python -m pipx` when running as `python -m pipx`

## 1.1.0

- Fix encoding issue on Windows when pip fails to install a package
Expand Down
14 changes: 13 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def print_version() -> None:
print(__version__)


def prog_name() -> str:
try:
prog = os.path.basename(sys.argv[0])
if prog == "__main__.py":
return f"{sys.executable} -m pipx"
else:
return prog
except Exception:
pass
return "pipx"


SPEC_HELP = textwrap.dedent(
"""\
The package name or specific installation source passed to pip.
Expand Down Expand Up @@ -669,7 +681,7 @@ def get_command_parser() -> argparse.ArgumentParser:
completer_venvs = InstalledVenvsCompleter(venv_container)

parser = argparse.ArgumentParser(
prog="pipx",
prog=prog_name(),
formatter_class=LineWrapRawTextHelpFormatter,
description=PIPX_DESCRIPTION,
)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ def test_version(monkeypatch, capsys):
captured = capsys.readouterr()
mock_exit.assert_called_with(0)
assert main.__version__ in captured.out.strip()


@pytest.mark.parametrize(
("argv", "executable", "expected"),
[
("/usr/bin/pipx", "", "pipx"),
("__main__.py", "/usr/bin/python", "/usr/bin/python -m pipx"),
],
)
def test_prog_name(monkeypatch, argv, executable, expected):
monkeypatch.setattr("pipx.main.sys.argv", [argv])
monkeypatch.setattr("pipx.main.sys.executable", executable)
assert main.prog_name() == expected

0 comments on commit 243c631

Please sign in to comment.