sky-meilin's picture
Create Python
d21f013 verified
Raw
History Blame Contribute Delete
508 Bytes
import subprocess
import uuid
async def run_python(code: str):
file_id = str(uuid.uuid4())
path = f"/tmp/{file_id}.py"
with open(path, "w") as f:
f.write(code)
try:
result = subprocess.run(
["python3", path],
capture_output=True,
text=True,
timeout=8
)
return {
"output": result.stdout or result.stderr
}
except Exception as e:
return {
"error": str(e)
}