forked from astral-sh/uv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_embedded_python.py
executable file
·96 lines (82 loc) · 3.3 KB
/
check_embedded_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""Install `pylint` and `numpy` into an embedded Python."""
import argparse
import logging
import os
import subprocess
import sys
import tempfile
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
parser = argparse.ArgumentParser(
description="Check an embedded Python interpreter."
)
parser.add_argument("--uv", help="Path to a uv binary.")
args = parser.parse_args()
uv: str = os.path.abspath(args.uv) if args.uv else "uv"
# Create a temporary directory.
with tempfile.TemporaryDirectory() as temp_dir:
# Create a virtual environment with `uv`.
logging.info("Creating virtual environment with `uv`...")
subprocess.run(
[uv, "venv", ".venv", "--seed", "--python", sys.executable],
cwd=temp_dir,
check=True,
)
if os.name == "nt":
executable = os.path.join(temp_dir, ".venv", "Scripts", "python.exe")
else:
executable = os.path.join(temp_dir, ".venv", "bin", "python")
logging.info("Querying virtual environment...")
subprocess.run(
[executable, "--version"],
cwd=temp_dir,
check=True,
)
logging.info("Installing into `uv` virtual environment...")
# Disable the `CONDA_PREFIX` and `VIRTUAL_ENV` environment variables, so that
# we only rely on virtual environment discovery via the `.venv` directory.
# Our "system Python" here might itself be a Conda environment!
env = os.environ.copy()
env["CONDA_PREFIX"] = ""
env["VIRTUAL_ENV"] = ""
# Install, verify, and uninstall a few packages.
for package in ["pylint", "numpy"]:
# Install the package.
logging.info(
f"Installing the package `{package}` into the virtual environment..."
)
subprocess.run(
[uv, "pip", "install", package, "--verbose"],
cwd=temp_dir,
check=True,
env=env,
)
# Ensure that the package is installed in the virtual environment.
logging.info(f"Checking that `{package}` is installed.")
code = subprocess.run(
[executable, "-c", f"import {package}"],
cwd=temp_dir,
)
if code.returncode != 0:
raise Exception(
f"The package `{package}` isn't installed in the virtual environment."
)
# Uninstall the package.
logging.info(f"Uninstalling the package `{package}`.")
subprocess.run(
[uv, "pip", "uninstall", package, "--verbose"],
cwd=temp_dir,
check=True,
env=env,
)
# Ensure that the package isn't installed in the virtual environment.
logging.info(f"Checking that `{package}` isn't installed.")
code = subprocess.run(
[executable, "-m", "pip", "show", package],
cwd=temp_dir,
)
if code.returncode == 0:
raise Exception(
f"The package `{package}` is installed in the virtual environment (but shouldn't be)."
)