From a2f2bae9dbbaa837bf820f12b0212e37cf0b4c5c Mon Sep 17 00:00:00 2001 From: Gitznik Date: Wed, 27 Dec 2023 18:18:51 +0100 Subject: [PATCH 1/7] Print more user friendly error when --python version not found --- CHANGELOG.md | 1 + src/pipx/commands/install.py | 21 ++++++++++++++++++++- src/pipx/constants.py | 1 + tests/test_install.py | 6 ++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2f546b3ad..a0f91af098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - [docs] Add Scoop installation instructions - Add ability to install multiple packages at once +- Raise more user friendly error when provided `--python` version is not found. ## 1.3.3 diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index 8e84bd309f..9b9fe33974 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -3,7 +3,12 @@ from pipx import constants 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 @@ -102,6 +107,20 @@ def install( include_dependencies, force=force, ) + except FileNotFoundError as e: + venv.remove_venv() + if python in str(e): + print( + ( + f"No executable for the provided Python version '{python}' found.\n" + "Please make sure the executable name is on your PATH/" + "the path to the executable is correct." + ) + ) + return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND + else: + print() + raise except (Exception, KeyboardInterrupt): print() venv.remove_venv() diff --git a/src/pipx/constants.py b/src/pipx/constants.py index dbe3a707cc..8aba427efd 100644 --- a/src/pipx/constants.py +++ b/src/pipx/constants.py @@ -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 diff --git a/tests/test_install.py b/tests/test_install.py index b814b736a0..a1a43f5c2f 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -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 From 34678d145dace4cb9d8de24d526d41a17edf7588 Mon Sep 17 00:00:00 2001 From: Gitznik Date: Fri, 29 Dec 2023 16:35:09 +0100 Subject: [PATCH 2/7] Fix printing nice error message on --python not found on windows --- src/pipx/commands/install.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index 9b9fe33974..2d62b1c918 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -109,7 +109,7 @@ def install( ) except FileNotFoundError as e: venv.remove_venv() - if python in str(e): + if python in str(e) or "The system cannot find the file specified" in str(e): print( ( f"No executable for the provided Python version '{python}' found.\n" From b88f0b22e03808fb4a622f3430c5c3affcd3f292 Mon Sep 17 00:00:00 2001 From: Robert <49005401+Gitznik@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:52:57 +0100 Subject: [PATCH 3/7] Update src/pipx/commands/install.py Co-authored-by: chrysle --- src/pipx/commands/install.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index 2d62b1c918..abc1c1eecb 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -111,11 +111,13 @@ def install( venv.remove_venv() if python in str(e) or "The system cannot find the file specified" in str(e): print( - ( - f"No executable for the provided Python version '{python}' found.\n" - "Please make sure the executable name is on your PATH/" - "the path to the executable is correct." - ) + pipx_wrap( + f""" + No executable for the provided Python version '{python}' found.\n + Please make sure the executable name is on your PATH/ + the path to the executable is correct. + """ + ) ) return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND else: From d59d0ee8779f40f40b3dddd9a2e556f14cf6f30c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Dec 2023 14:53:07 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pipx/commands/install.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index abc1c1eecb..f74d66e0cb 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -111,13 +111,13 @@ def install( venv.remove_venv() if python in str(e) or "The system cannot find the file specified" in str(e): print( - pipx_wrap( - f""" + pipx_wrap( + f""" No executable for the provided Python version '{python}' found.\n Please make sure the executable name is on your PATH/ the path to the executable is correct. """ - ) + ) ) return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND else: From 816d7e39c5c11805f18638d2dcc7e60f2d7d04ed Mon Sep 17 00:00:00 2001 From: Gitznik Date: Sat, 30 Dec 2023 16:02:46 +0100 Subject: [PATCH 5/7] Format error message --- src/pipx/commands/install.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index f74d66e0cb..43c668ba34 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -113,10 +113,10 @@ def install( print( pipx_wrap( f""" - No executable for the provided Python version '{python}' found.\n - Please make sure the executable name is on your PATH/ + 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. - """ + """ ) ) return EXIT_CODE_SPECIFIED_PYTHON_EXECUTABLE_NOT_FOUND From 3842342d4b5ff7981359400b9f1183ff11fa2de3 Mon Sep 17 00:00:00 2001 From: Gitznik Date: Tue, 2 Jan 2024 18:38:45 +0100 Subject: [PATCH 6/7] Format error message --- src/pipx/commands/install.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pipx/commands/install.py b/src/pipx/commands/install.py index 43c668ba34..6b34f53862 100644 --- a/src/pipx/commands/install.py +++ b/src/pipx/commands/install.py @@ -1,7 +1,7 @@ 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, @@ -113,10 +113,11 @@ def install( print( pipx_wrap( f""" - No executable for the provided Python version '{python}' found. + {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 From 9b3a174306e4d7be28f024bf5c8de2b5403201b8 Mon Sep 17 00:00:00 2001 From: Gitznik Date: Tue, 2 Jan 2024 18:42:46 +0100 Subject: [PATCH 7/7] Move changelog to correct section --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1d4c1ccc8..cd9c4393cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## dev - 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 @@ -10,7 +11,6 @@ - Add `--quiet` and `--verbose` options for the `pipx` subcommands - [docs] Add Scoop installation instructions - Add ability to install multiple packages at once -- Raise more user friendly error when provided `--python` version is not found. ## 1.3.3