Skip to content

Commit d9cb03e

Browse files
authored
Merge pull request #67 from DavidCEllis/fix_test_issue
Improve base python discovery logic
2 parents 006e114 + 2d0731f commit d9cb03e

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/ducktools/pythonfinder/venv.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# ducktools-pythonfinder
22
# MIT License
3-
#
3+
#
44
# Copyright (c) 2023-2025 David C Ellis
5-
#
5+
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
88
# in the Software without restriction, including without limitation the rights
99
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
# copies of the Software, and to permit persons to whom the Software is
1111
# furnished to do so, subject to the following conditions:
12-
#
12+
#
1313
# The above copyright notice and this permission notice shall be included in all
1414
# copies or substantial portions of the Software.
15-
#
15+
#
1616
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -90,10 +90,16 @@ def parent_executable(self) -> str | None:
9090
if sys.platform == "win32":
9191
parent_exe = os.path.join(self.parent_path, "python.exe")
9292
else:
93-
# try with additional numbers in order eg: python313, python3, python
94-
for i in reversed(range(2)):
95-
version_part = "".join(str(v) for v in self.version[:i])
96-
parent_exe = os.path.join(self.parent_path, f"python{version_part}")
93+
# try with additional numbers in order eg: python3.13, python313, python3, python
94+
suffixes = [
95+
f"{self.version[0]}.{self.version[1]}",
96+
f"{self.version[0]}{self.version[1]}",
97+
f"{self.version[0]}",
98+
""
99+
]
100+
101+
for suffix in suffixes:
102+
parent_exe = os.path.join(self.parent_path, f"python{suffix}")
97103
if os.path.exists(parent_exe):
98104
break
99105

tests/conftest.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# ducktools-pythonfinder
22
# MIT License
3-
#
3+
#
44
# Copyright (c) 2023-2025 David C Ellis
5-
#
5+
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
88
# in the Software without restriction, including without limitation the rights
99
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
# copies of the Software, and to permit persons to whom the Software is
1111
# furnished to do so, subject to the following conditions:
12-
#
12+
#
1313
# The above copyright notice and this permission notice shall be included in all
1414
# copies or substantial portions of the Software.
15-
#
15+
#
1616
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -60,7 +60,8 @@ def this_python(temp_finder):
6060
elif sys.platform == "win32":
6161
exename = "python.exe"
6262
else:
63-
exename = "python"
63+
ver = ".".join(str(v) for v in sys.version_info[:2])
64+
exename = f"python{ver}"
6465

6566
if sys.platform == "win32":
6667
py_exe = Path(sys.base_prefix) / exename
@@ -72,10 +73,7 @@ def this_python(temp_finder):
7273

7374
@pytest.fixture(scope="function")
7475
def this_venv(temp_finder):
75-
if sys.platform == "win32":
76-
exe = sys.executable
77-
else:
78-
exe = str(Path(sys.executable).with_name("python"))
76+
exe = sys.executable
7977
venv = temp_finder.query_install(exe)
8078
return venv
8179

tests/test_venv_finder.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# ducktools-pythonfinder
22
# MIT License
3-
#
3+
#
44
# Copyright (c) 2023-2025 David C Ellis
5-
#
5+
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
88
# in the Software without restriction, including without limitation the rights
99
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
# copies of the Software, and to permit persons to whom the Software is
1111
# furnished to do so, subject to the following conditions:
12-
#
12+
#
1313
# The above copyright notice and this permission notice shall be included in all
1414
# copies or substantial portions of the Software.
15-
#
15+
#
1616
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -23,7 +23,9 @@
2323
import os
2424
import subprocess
2525
import sys
26+
import sysconfig
2627
import tempfile
28+
from pathlib import Path
2729

2830
from ducktools.pythonfinder.venv import list_python_venvs
2931

@@ -35,15 +37,26 @@ def with_venvs():
3537
with tempfile.TemporaryDirectory() as tmpdir:
3638
# We can't actually use venv directly here as
3739
# Older python on linux makes invalid venvs
40+
41+
config_exe = sysconfig.get_config_var("EXENAME")
42+
43+
if config_exe:
44+
exename = os.path.basename(config_exe)
45+
elif sys.platform == "win32":
46+
exename = "python.exe"
47+
else:
48+
ver = ".".join(str(v) for v in sys.version_info[:2])
49+
exename = f"python{ver}"
50+
3851
if sys.platform == "win32":
39-
python_exe = os.path.join(sys.base_prefix, "python.exe")
52+
py_exe = Path(sys.base_prefix) / exename
4053
else:
41-
python_exe = os.path.join(sys.base_prefix, "bin", "python")
54+
py_exe = Path(sys.base_prefix) / "bin" / exename
4255

4356
def make_venv(pth):
4457
subprocess.run(
4558
[
46-
python_exe,
59+
py_exe,
4760
"-m", "venv",
4861
"--without-pip",
4962
os.path.join(tmpdir, pth),

0 commit comments

Comments
 (0)