From 847d53ee9d63712499eb8e34dddc185b0a2fe41d Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 4 Dec 2023 12:13:36 +0100 Subject: [PATCH 1/2] flit: improve the --python option pip --python option can take both the path to a Python executable or the path to a virtual environment, greatly improving the user experience. Teach flit to do the same. --- flit/__init__.py | 9 +++++++++ tests/test_find_python_executable.py | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/flit/__init__.py b/flit/__init__.py index 055cfb99..47252839 100644 --- a/flit/__init__.py +++ b/flit/__init__.py @@ -26,6 +26,15 @@ def find_python_executable(python: Optional[str] = None) -> str: python = os.environ.get("FLIT_INSTALL_PYTHON") if not python: return sys.executable + if os.path.isdir(python): + # Assume it's a virtual environment and look for the environment's + # Python executable. This is the same behavior used by pip. + # + # Try both Unix and Windows paths in case of odd cases like cygwin. + for exe in ("bin/python", "Scripts/python.exe"): + py = os.path.join(python, exe) + if os.path.exists(py): + python = py if os.path.isabs(python): # sys.executable is absolute too return python # get absolute filepath of {python} diff --git a/tests/test_find_python_executable.py b/tests/test_find_python_executable.py index 161dc7a2..b3bf563f 100644 --- a/tests/test_find_python_executable.py +++ b/tests/test_find_python_executable.py @@ -1,6 +1,7 @@ -import os +from os.path import isabs, basename, dirname import re import sys +import venv import pytest @@ -20,7 +21,23 @@ def test_abs(): def test_find_in_path(): - assert os.path.isabs(find_python_executable("python")) + assert isabs(find_python_executable("python")) + + +def test_env(tmp_path): + path = tmp_path / "venv" + venv.create(path) + + executable = find_python_executable(path) + assert basename(dirname(dirname(executable))) == "venv" + + +def test_env_abs(tmp_path, monkeypatch): + path = tmp_path / "venv" + venv.create(path) + + monkeypatch.chdir(tmp_path) + assert isabs(find_python_executable("venv")) @pytest.mark.parametrize("bad_python_name", ["pyhton", "ls", "."]) From 05133e9c8a6f795aadcc409b8e2a761278e5ff1f Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Sat, 27 Jan 2024 18:45:49 +0000 Subject: [PATCH 2/2] Return Python from venv path and skip which() step --- flit/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flit/__init__.py b/flit/__init__.py index 47252839..68768d29 100644 --- a/flit/__init__.py +++ b/flit/__init__.py @@ -34,7 +34,7 @@ def find_python_executable(python: Optional[str] = None) -> str: for exe in ("bin/python", "Scripts/python.exe"): py = os.path.join(python, exe) if os.path.exists(py): - python = py + return os.path.abspath(py) if os.path.isabs(python): # sys.executable is absolute too return python # get absolute filepath of {python}