Skip to content

Commit 846ed52

Browse files
committed
GH-87915: create a venv executable with the same name as sys.executable
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 10ee2d9 commit 846ed52

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Lib/test/test_venv.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,17 @@ def test_venvwlauncher(self):
888888
except subprocess.CalledProcessError:
889889
self.fail("venvwlauncher.exe did not run %s" % exename)
890890

891+
def test_ensure_sys_executable_name(self):
892+
"""
893+
Test that we create a executable with the same name as sys.executable.
894+
"""
895+
rmtree(self.env_dir)
896+
executable_dir = os.path.dirname(sys.executable)
897+
with patch('sys.executable', os.path.join(executable_dir, 'some-custom-name')):
898+
venv.create(self.env_dir)
899+
scripts_dir = os.path.join(self.env_dir, self.bindir)
900+
self.assertIn('some-custom-name', os.listdir(scripts_dir))
901+
891902

892903
@requireVenvCreate
893904
class EnsurePipTest(BaseTest):

Lib/venv/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,14 @@ def setup_python(self, context):
314314
if not os.path.islink(path):
315315
os.chmod(path, 0o755)
316316

317-
suffixes = ['python', 'python3', f'python3.{sys.version_info[1]}']
317+
suffixes = {
318+
'python',
319+
'python3',
320+
f'python3.{sys.version_info[1]}',
321+
os.path.basename(sys.executable),
322+
}
318323
if sys.version_info[:2] == (3, 14):
319-
suffixes.append('𝜋thon')
324+
suffixes.add('𝜋thon')
320325
for suffix in suffixes:
321326
path = os.path.join(binpath, suffix)
322327
if not os.path.exists(path):
@@ -388,6 +393,10 @@ def setup_python(self, context):
388393
f'pythonw{exe_t}{exe_d}.exe': pythonw_exe,
389394
}
390395

396+
for sources_dict in (link_sources, copy_sources):
397+
if exename not in sources_dict:
398+
sources_dict[exename] = python_exe
399+
391400
do_copies = True
392401
if self.symlinks:
393402
do_copies = False
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
We now make sure :mod:`venv` creates an executable with the same name as
2+
:data:`sys.executable` in environments.

0 commit comments

Comments
 (0)