用途
python-executor 用于在安全的沙箱环境中执行 Python 代码,底层通过 inference.sh 提供能力。它适合需要运行 Python 脚本、但不想在本地配置完整环境的场景,尤其是网页抓取、数据处理、图像/视频处理、3D 模型处理和自动化等任务。
主要能力与运行环境
- 环境:Python 3.10,仅 CPU。
- 内存:默认 8GB;可使用 high_memory 变体获得 16GB。
- 超时:1-300 秒,默认 30 秒。
- 安全执行:在隔离的子进程中运行。
- 非交互式:图形需使用 plt.savefig() 等保存方式,而非 plt.show()。
- 文件输出:保存到 outputs/ 的文件会被自动检测并返回。
预装库
- 网页抓取与 HTTP:requests、httpx、aiohttp、beautifulsoup4、lxml、selenium、playwright、scrapy。
- 数据处理:numpy、pandas、scipy。
- 可视化:matplotlib、seaborn、plotly。
- 图像处理:pillow、opencv-python-headless、scikit-image、imageio。
- 视频与音频:moviepy、av(PyAV)、ffmpeg-python、pydub。
- 3D 处理:trimesh、open3d、numpy-stl、meshio、pyvista。
- 文档与图形:svgwrite、cairosvg、reportlab、pypdf2。
使用方式
需先安装 belt CLI 技能:
npx skills add belt-sh/cli
然后登录:
belt login
运行 Python 代码示例:
belt app run infsh/python-executor --input '{
"code": "import pandas as pd\nprint(pd.version)"
}'
默认(8GB RAM):
belt app run infsh/python-executor --input input.json
高内存(16GB RAM):
belt app run infsh/python-executor@high_memory --input input.json
输入 Schema
{
"code": "print('Hello World!')",
"timeout": 30,
"capture_output": true,
"working_dir": null
}
示例
网页抓取:
belt app run infsh/python-executor --input '{
"code": "import requests\nfrom bs4 import BeautifulSoup\n\nresponse = requests.get(\"https://example.com\")\nsoup = BeautifulSoup(response.content, \"html.parser\")\nprint(soup.find(\"title\").text)"
}'
数据分析与可视化:
belt app run infsh/python-executor --input '{
"code": "import pandas as pd\nimport matplotlib.pyplot as plt\n\ndata = {\"name\": [\"Alice\", \"Bob\"], \"sales\": [100, 150]}\ndf = pd.DataFrame(data)\n\nplt.bar(df[\"name\"], df[\"sales\"])\nplt.savefig(\"outputs/chart.png\")\nprint(\"Chart saved!\")"
}'
图像处理:
belt app run infsh/python-executor --input '{
"code": "from PIL import Image\nimport numpy as np\n\n# Create gradient image\narr = np.linspace(0, 255, 256*256, dtype=np.uint8).reshape(256, 256)\nimg = Image.fromarray(arr, mode=\"L\")\nimg.save(\"outputs/gradient.png\")\nprint(\"Image created!\")"
}'
视频生成:
belt app run infsh/python-executor --input '{
"code": "from moviepy.editor import ColorClip, TextClip, CompositeVideoClip\n\nclip = ColorClip(size=(640, 480), color=(0, 100, 200), duration=3)\ntxt = TextClip(\"Hello!\", fontsize=70, color=\"white\").set_position(\"center\").set_duration(3)\nvideo = CompositeVideoClip([clip, txt])\nvideo.write_videofile(\"outputs/hello.mp4\", fps=24)\nprint(\"Video created!\")",
"timeout": 120
}'
3D 模型处理:
belt app run infsh/python-executor --input '{
"code": "import trimesh\n\nsphere = trimesh.creation.icosphere(subdivisions=3, radius=1.0)\nsphere.export(\"outputs/sphere.stl\")\nprint(f\"Created sphere with {len(sphere.vertices)} vertices\")"
}'
API 调用:
belt app run infsh/python-executor --input '{
"code": "import requests\nimport json\n\nresponse = requests.get(\"https://api.github.com/users/octocat\")\ndata = response.json()\nprint(json.dumps(data, indent=2))"
}'
文件输出:保存到 outputs/ 的文件会自动返回。
plt.savefig('outputs/chart.png')
df.to_csv('outputs/data.csv')
video.write_videofile('outputs/video.mp4')
mesh.export('outputs/model.stl')
适用场景
- 网页抓取:从网站提取数据。
- 数据分析:处理和可视化数据集。
- 图像处理:缩放、裁剪、合成图像。
- 视频生成:生成带文字叠加的视频。
- 3D 处理:加载、变换、导出 3D 模型。
- API 集成:调用外部 API。
- PDF 生成:创建报告和文档。
- 自动化:运行任意 Python 脚本。
限制
- 仅 CPU,不包含 GPU/机器学习库;这类任务需使用专用的 AI 应用。
- 非交互式环境,不能使用 plt.show() 等交互式展示。
- 超时上限 300 秒。
- 收费、价格等细节暂未提供。