Skip to content

Commit 7c59e54

Browse files
committed
Add virtual environment and Python interpreter tracking
1 parent f906491 commit 7c59e54

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

python/ty/__main__.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,48 @@
66
from ty import find_ty_bin
77

88

9+
def _detect_virtualenv() -> str:
10+
"""
11+
Find the virtual environment path for the current Python executable.
12+
"""
13+
14+
# If it's already set, then just use it
15+
value = os.getenv("VIRTUAL_ENV")
16+
if value:
17+
return value
18+
19+
# Otherwise, check if we're in a venv
20+
venv_marker = os.path.join(sys.prefix, "pyvenv.cfg")
21+
22+
if os.path.exists(venv_marker):
23+
return sys.prefix
24+
25+
return ""
26+
27+
928
def _run() -> None:
1029
ty = find_ty_bin()
1130

31+
env = os.environ.copy()
32+
venv = _detect_virtualenv()
33+
if venv:
34+
env.setdefault("VIRTUAL_ENV", venv)
35+
36+
# Let `ty` know that it was spawned by this Python interpreter
37+
env["TY__PARENT_INTERPRETER"] = sys.executable
38+
1239
if sys.platform == "win32":
1340
import subprocess
1441

1542
# Avoid emitting a traceback on interrupt
1643
try:
17-
completed_process = subprocess.run([ty, *sys.argv[1:]])
44+
completed_process = subprocess.run([ty, *sys.argv[1:]], env=env)
1845
except KeyboardInterrupt:
1946
sys.exit(2)
2047

2148
sys.exit(completed_process.returncode)
2249
else:
23-
os.execvp(ty, [ty, *sys.argv[1:]])
50+
os.execvpe(ty, [ty, *sys.argv[1:]], env=env)
2451

2552

2653
if __name__ == "__main__":

ruff

Submodule ruff updated 457 files

0 commit comments

Comments
 (0)