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

如何创建或转换可构建、运行测试及执行Python脚本的Linux可执行文件?

Alright, let's walk through how to solve both of your requirements—these are super common when you want to bundle development workflows into single, easy-to-run tools on Linux. I'll break it down step by step for each case.


1. Create a Linux Executable That Handles Build, Test, and Python Script Execution

The simplest starting point is a shell script—it's lightweight, easy to tweak, and natively executable on Linux. Here's how to build one:

Step 1: Write the Shell Script

Create a file named workflow-runner.sh with this structure (swap out the placeholder commands for your actual workflow):

#!/bin/bash

# Helper to check if commands succeed
check_exit_code() {
    if [ $? -ne 0 ]; then
        echo "$1 failed! Exiting."
        exit 1
    fi
}

# Run build process
echo "=== Starting Build ==="
make build  # Replace with your build command (e.g., npm install, cmake --build .)
check_exit_code "Build"

# Run tests
echo -e "\n=== Running Tests ==="
pytest tests/  # Replace with your test command (e.g., unittest, go test)
check_exit_code "Tests"

# Execute your Python script
echo -e "\n=== Running Python Script ==="
python3 /path/to/your/script.py --flag value  # Replace with your script path/args
check_exit_code "Python Script"

echo -e "\nAll tasks completed successfully!"

Step 2: Make It Executable

Grant execution permissions so Linux recognizes it as a runnable file:

chmod +x workflow-runner.sh

You can now run it directly with ./workflow-runner.sh.

Step 3 (Optional): Compile to a Binary

If you want a compiled binary (to hide source code or improve portability), use shc (Shell Script Compiler):

  1. Install shc first:
    sudo apt install shc  # Debian/Ubuntu
    # For RHEL/CentOS: sudo yum install shc
    
  2. Compile the script:
    shc -f workflow-runner.sh -o workflow-runner
    

This generates a standalone binary workflow-runner that acts exactly like the original script.


2. Convert a Python Script Into a Linux Executable for Build, Test, and Program Run Tasks

For this, we'll first write a Python script that handles all your workflow logic, then package it into a standalone executable using PyInstaller.

Step 1: Write the Python Workflow Script

Create a file named workflow-manager.py with this logic (update placeholders to match your setup):

import subprocess
import sys

def run_command(cmd: str, description: str):
    """Run a shell command and handle errors cleanly"""
    print(f"\n=== {description} ===")
    try:
        result = subprocess.run(
            cmd,
            shell=True,
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"Error during {description}:")
        print(e.stderr)
        sys.exit(1)

def main():
    # Run build step
    run_command("make build", "Building Project")

    # Run tests
    run_command("pytest tests/", "Running Tests")

    # Run your compiled program
    run_command("./your-program-binary", "Running Program")

    print("\nAll workflow steps completed successfully!")

if __name__ == "__main__":
    main()

Step 2: Install PyInstaller

PyInstaller packages Python code into standalone executables:

pip install pyinstaller

Step 3: Package the Script into an Executable

Run this command to create a single binary file:

pyinstaller --onefile workflow-manager.py

A dist/ folder will be created with your executable. You can run it directly:

./dist/workflow-manager

Important Notes

  • PyInstaller only packages your Python code—make sure target machines have any system dependencies (like make, pytest) installed.
  • For extra customization (e.g., hiding console output, adding icons), use flags like --windowed (for GUI apps) or --name to rename the executable.

内容的提问来源于stack exchange,提问作者Rohit Anil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:09:37