From d2427b9d272898a2c4c122d35ebb8eda1182151b Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Mon, 16 Dec 2024 12:50:13 +0100 Subject: [PATCH] Try copying Windows executable --- conda_lock/conda_solver.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/conda_lock/conda_solver.py b/conda_lock/conda_solver.py index a2c02fed..dc4c891d 100644 --- a/conda_lock/conda_solver.py +++ b/conda_lock/conda_solver.py @@ -3,6 +3,7 @@ import os import pathlib import shlex +import shutil import subprocess import sys import tempfile @@ -619,21 +620,32 @@ def make_fake_python_binary(prefix: str) -> None: print(stderr_message, file=sys.stderr, flush=True, end='') - if "-m pip" not in cmd: + if "-m pip" in cmd: + # Simulate an empty `pip inspect` output + print('{}', flush=True) + else: raise RuntimeError("Expected to invoke pip module with `-m pip`.") """ ) ) if sys.platform == "win32": - # On Windows, create a batch file that calls the script - fake_python_binary = pathlib.Path(prefix) / "Scripts" / "python.bat" + # On Windows, copy sys.executable to prefix/Scripts/python.exe + fake_python_binary = pathlib.Path(prefix) / "Scripts" / "python.exe" fake_python_binary.parent.mkdir(parents=True, exist_ok=True) - batch_content = dedent(f"""\ + + # Copy the existing python.exe into the fake environment + shutil.copyfile(sys.executable, fake_python_binary) + + # Adjust the environment to ensure our fake script is executed + # Create a wrapper batch file that sets PYTHONPATH + wrapper_batch = pathlib.Path(prefix) / "Scripts" / "python.bat" + wrapper_batch_content = dedent(f"""\ @echo off - "{sys.executable}" "{fake_python_script}" %* + set PYTHONPATH={prefix};%PYTHONPATH% + "{fake_python_binary}" %* """) - fake_python_binary.write_text(batch_content) + wrapper_batch.write_text(wrapper_batch_content) else: # On Unix-like systems, create a shell script that calls the script fake_python_binary = pathlib.Path(prefix) / "bin" / "python"