From 809e98c54f223ab9a02a49cb59f77fe18d15ab59 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Thu, 6 Apr 2023 21:27:30 +0100 Subject: [PATCH] use shutil.which() to detect the active python --- src/poetry/utils/env.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index d5a96fac4c2..2a1074c5b9e 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -9,6 +9,7 @@ import platform import plistlib import re +import shutil import subprocess import sys import sysconfig @@ -532,19 +533,23 @@ def _full_python_path(python: str) -> Path: @staticmethod def _detect_active_python(io: None | IO = None) -> Path | None: io = io or NullIO() + io.write_error_line( + ( + "Trying to detect current active python executable as specified in" + " the config." + ), + verbosity=Verbosity.VERBOSE, + ) + executable = None + python = shutil.which("python") + if python is not None: + with contextlib.suppress(EnvCommandError): + executable = EnvManager._full_python_path(python) - try: - io.write_error_line( - ( - "Trying to detect current active python executable as specified in" - " the config." - ), - verbosity=Verbosity.VERBOSE, - ) - executable = EnvManager._full_python_path("python") + if executable is not None: io.write_error_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE) - except EnvCommandError: + else: io.write_error_line( ( "Unable to detect the current active python executable. Falling" @@ -552,6 +557,7 @@ def _detect_active_python(io: None | IO = None) -> Path | None: ), verbosity=Verbosity.VERBOSE, ) + return executable @staticmethod