Context manager that creates a virtualenv in a temporary directory Returns the path to the created Python executable
(python_executable: str = sys.executable)
| 29 | |
| 30 | @contextmanager |
| 31 | def virtualenv(python_executable: str = sys.executable) -> Iterator[tuple[str, str]]: |
| 32 | """Context manager that creates a virtualenv in a temporary directory |
| 33 | |
| 34 | Returns the path to the created Python executable |
| 35 | """ |
| 36 | with tempfile.TemporaryDirectory() as venv_dir: |
| 37 | proc = subprocess.run( |
| 38 | [python_executable, "-m", "venv", venv_dir], cwd=os.getcwd(), capture_output=True |
| 39 | ) |
| 40 | if proc.returncode != 0: |
| 41 | err = proc.stdout.decode("utf-8") + proc.stderr.decode("utf-8") |
| 42 | raise Exception("Failed to create venv.\n" + err) |
| 43 | if sys.platform == "win32": |
| 44 | yield venv_dir, os.path.abspath(os.path.join(venv_dir, "Scripts", "python")) |
| 45 | else: |
| 46 | yield venv_dir, os.path.abspath(os.path.join(venv_dir, "bin", "python")) |
| 47 | |
| 48 | |
| 49 | def upgrade_pip(python_executable: str) -> None: |
no test coverage detected
searching dependent graphs…