Skip to content

Commit

Permalink
Print more user friendly error when --python version not found (#1171)
Browse files Browse the repository at this point in the history
Co-authored-by: chrysle <[email protected]>
Co-authored-by: Bernát Gábor <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 3, 2024
1 parent 2ca518e commit 2b131cb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.4.1

- Set default logging level to WARNING, so debug log messages won't be shown without passing additional flags such as `--verbose`
- Raise more user friendly error when provided `--python` version is not found.

## 1.4.0

Expand Down
26 changes: 24 additions & 2 deletions src/pipx/commands/install.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from pathlib import Path
from typing import List, Optional

from pipx import constants
from pipx import constants, emojis
from pipx.commands.common import package_name_from_spec, run_post_install_actions
from pipx.constants import EXIT_CODE_INSTALL_VENV_EXISTS, EXIT_CODE_OK, ExitCode
from pipx.constants import (
EXIT_CODE_INSTALL_VENV_EXISTS,
EXIT_CODE_OK,
EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND,
ExitCode,
)
from pipx.interpreter import DEFAULT_PYTHON
from pipx.util import pipx_wrap
from pipx.venv import Venv, VenvContainer
Expand Down Expand Up @@ -102,6 +107,23 @@ def install(
include_dependencies,
force=force,
)
except FileNotFoundError as e:
venv.remove_venv()
if python in str(e) or "The system cannot find the file specified" in str(e):
print(
pipx_wrap(
f"""
{emojis.hazard} No executable for the provided Python version '{python}' found.
Please make sure the executable name is on your PATH /
the path to the executable is correct.
""",
subsequent_indent=" " * 4,
)
)
return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND
else:
print()
raise
except (Exception, KeyboardInterrupt):
print()
venv.remove_venv()
Expand Down
1 change: 1 addition & 0 deletions src/pipx/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
EXIT_CODE_UNINSTALL_ERROR = ExitCode(1)
EXIT_CODE_REINSTALL_VENV_NONEXISTENT = ExitCode(1)
EXIT_CODE_REINSTALL_INVALID_PYTHON = ExitCode(1)
EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND = ExitCode(1)

pipx_log_file: Optional[Path] = None

Expand Down
6 changes: 6 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,9 @@ def test_passed_python_and_force_flag_warning(pipx_temp_env, capsys):
assert not run_pipx_cli(["install", "pycowsay", "--force"])
captured = capsys.readouterr()
assert "--python is ignored when --force is passed." not in captured.out


def test_passed_python_not_executable(pipx_temp_env, capsys):
assert run_pipx_cli(["install", "--python", "py_not_real", "pycowsay"])
captured = capsys.readouterr()
assert "No executable for the provided Python version" in captured.out

0 comments on commit 2b131cb

Please sign in to comment.