You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中以当前环境调用外部脚本并传递参数、获取打印输出?

How to Run a GitHub-hosted Python Tool Repeatedly with Different Args and Capture Output in Python

Great question! I’ve dealt with exactly this scenario before—needing to wrap an external Python tool (with tons of dependencies and code I didn’t want to mess with) in a loop, pass varying arguments each time, and grab its console output. The good news is Python’s standard library has everything you need to pull this off cleanly.

The subprocess module is Python’s go-to for spawning external processes, and run() is the simplest way to execute a command, capture its output, and check its exit status.

First, make sure you’ve cloned the GitHub tool’s repository and installed its dependencies (usually with pip install -r requirements.txt). Then, you can call the tool like this in your wrapper script:

import subprocess
import sys

# Define the path to the GitHub tool's main script
TOOL_PATH = "./path/to/github_tool/main_script.py"

# List of different argument sets to pass each iteration
argument_sets = [
    ["--input", "file1.txt", "--output", "result1.json"],
    ["--input", "file2.txt", "--verbose"],
    ["--help"]  # Test with help argument
]

for args in argument_sets:
    print(f"\n=== Running tool with args: {' '.join(args)} ===")
    try:
        # Run the tool, capture stdout/stderr as strings
        result = subprocess.run(
            [sys.executable, TOOL_PATH] + args,
            capture_output=True,
            text=True,
            check=True  # Raises error if tool exits with non-zero code
        )
        
        # Access the captured output
        print("Tool stdout:")
        print(result.stdout)
        
        # Check stderr for warnings/errors if needed
        if result.stderr:
            print("\nTool stderr:")
            print(result.stderr)
            
    except subprocess.CalledProcessError as e:
        print(f"Tool failed with exit code {e.returncode}")
        print("Error output:")
        print(e.stderr)
    except FileNotFoundError:
        print(f"Error: Could not find the tool script at {TOOL_PATH}")

Key Notes:

  • sys.executable ensures we use the same Python interpreter as our wrapper script—critical for matching dependencies.
  • capture_output=True captures both stdout and stderr (alternatively, use stdout=subprocess.PIPE and stderr=subprocess.PIPE explicitly).
  • text=True (or encoding="utf-8") converts byte output to human-readable strings.
  • check=True will raise an error if the tool exits with a non-zero code, which you can catch to handle failures gracefully.

Approach 2: Use subprocess.Popen() for Real-Time Output

If the tool runs for a long time and you want to read its output as it’s generated (instead of waiting for it to finish), use Popen():

import subprocess
import sys

TOOL_PATH = "./path/to/github_tool/main_script.py"
argument_sets = [["--input", "large_file.txt"]]

for args in argument_sets:
    print(f"\n=== Running tool with real-time output ===")
    process = subprocess.Popen(
        [sys.executable, TOOL_PATH] + args,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,  # Line-buffered output
        universal_newlines=True
    )
    
    # Read stdout line by line as it's produced
    for line in process.stdout:
        print(f"Tool output: {line.strip()}")
        
    # Wait for process to finish and get exit code
    exit_code = process.wait()
    
    # Read any remaining stderr output
    stderr_output = process.stderr.read()
    if stderr_output:
        print(f"\nTool errors/warnings:\n{stderr_output}")
        
    if exit_code != 0:
        print(f"Tool exited with code {exit_code}")

Important Considerations

  • Working Directory: If the tool expects to run from its own repository folder, add cwd="./path/to/github_tool/repo" to subprocess.run() or Popen().
  • Environment Variables: If the tool relies on specific env vars, pass an env parameter (e.g., env=os.environ.copy() to inherit your current env, then modify it as needed).
  • Dependencies: Double-check that the tool’s dependencies are installed in the same Python environment as your wrapper script—use a virtual environment if needed to avoid conflicts.

This approach keeps the original tool untouched, uses Python’s standard library, and gives you full control over execution and output capture.

内容的提问来源于stack exchange,提问作者Maksim Khaitovich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:16:06