Skip to content

Commit 94d562f

Browse files
committed
fix: windows interpreter not found error
1 parent 63afc1b commit 94d562f

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

valthon/cli.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/env python3
22
import argparse
33
import os
4+
import subprocess
45
import sys
56
from pathlib import Path
67

@@ -157,21 +158,43 @@ def main() -> None:
157158
return
158159

159160
# Run file
160-
python_command = "python2" if cmd_args.python2 else "python3"
161+
if cmd_args.python2:
162+
python_commands = ["python2", "py -2", sys.executable]
163+
else:
164+
python_commands = ["python3", "python", "py", sys.executable]
165+
if os.name == "nt":
166+
python_commands.pop(0)
161167

162168
filename = Path(cmd_args.input[0]).name
169+
py_file = path_prefix + parser._change_file_name(filename, None)
170+
args_str = " ".join(arg for arg in cmd_args.args)
163171

164172
try:
165173
logger.log_info("Running")
166174
logger.program_header()
167-
os.system(
168-
"%s %s %s"
169-
% (
170-
python_command,
171-
path_prefix + parser._change_file_name(filename, None),
172-
" ".join(arg for arg in cmd_args.args),
173-
),
174-
)
175+
176+
# Try different Python commands until one works
177+
success = False
178+
for cmd in python_commands:
179+
try:
180+
if os.name == "nt":
181+
result = subprocess.run(
182+
f"{cmd} {py_file} {args_str}",
183+
shell=True,
184+
check=False,
185+
)
186+
else:
187+
result = subprocess.run([cmd, py_file, *cmd_args.args], check=False)
188+
189+
if result.returncode == 0:
190+
success = True
191+
break
192+
except:
193+
continue
194+
195+
if not success:
196+
logger.log_error("Could not find a working Python interpreter")
197+
175198
logger.program_footer()
176199

177200
except Exception as e:

0 commit comments

Comments
 (0)