Skip to content

Commit

Permalink
Merge pull request #667 from perillo/improve-venv-support
Browse files Browse the repository at this point in the history
flit: improve the --python option
  • Loading branch information
takluyver committed Jan 28, 2024
2 parents 792500a + 05133e9 commit 8ead22d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions flit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
return os.path.abspath(py)
if os.path.isabs(python): # sys.executable is absolute too
return python
# get absolute filepath of {python}
Expand Down
21 changes: 19 additions & 2 deletions tests/test_find_python_executable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from os.path import isabs, basename, dirname
import re
import sys
import venv

import pytest

Expand All @@ -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", "."])
Expand Down

0 comments on commit 8ead22d

Please sign in to comment.