蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行未命名 Python 代码执行与 bluesky-demo 工作区 test.py 本地子进程校验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-start.py
explore-start.py87 行
"""Offline VS Code open folder & Python file execution verifier for Ch09 (start)."""
import json
import subprocess
import sys
import tempfile
from pathlib import Path

VERSION = "CPython 3.12.14 offline VS Code workspace & Python execution verifier"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    # Case 1: Untitled buffer -> Python language mode -> execute print("Hello BlueSky")
    buffer_meta = {"name": "Untitled-1", "language_mode": "python", "code": 'print("Hello BlueSky")\n'}
    proc1 = subprocess.run(
        [sys.executable, "-I", "-c", buffer_meta["code"]],
        capture_output=True,
        encoding="utf-8",
        timeout=15,
    )
    cases.append(
        check(
            "untitled_python_language_and_exec",
            "Verify untitled Python buffer language assignment and print('Hello BlueSky') execution output",
            {
                "language_mode": "python",
                "returncode": 0,
                "stdout": "Hello BlueSky",
            },
            {
                "language_mode": buffer_meta["language_mode"],
                "returncode": proc1.returncode,
                "stdout": proc1.stdout.strip(),
            },
        )
    )

    # Case 2: Open workspace folder bluesky-demo with .vscode/ and execute test.py
    with tempfile.TemporaryDirectory() as tmp:
        ws = Path(tmp) / "bluesky-demo"
        (ws / ".vscode").mkdir(parents=True)
        (ws / ".vscode" / "settings.json").write_text('{"python.terminal.activateEnvironment": true}\n', encoding="utf-8")
        test_py = ws / "test.py"
        test_py.write_text('print("Hello BlueSky")\n', encoding="utf-8")
        proc2 = subprocess.run(
            [sys.executable, "-I", str(test_py)],
            cwd=ws,
            capture_output=True,
            encoding="utf-8",
            timeout=15,
        )
        cases.append(
            check(
                "workspace_folder_and_test_py_run",
                "Verify opening bluesky-demo workspace with .vscode folder and running test.py",
                {
                    "has_vscode_dir": True,
                    "script_exists": True,
                    "returncode": 0,
                    "stdout": "Hello BlueSky",
                },
                {
                    "has_vscode_dir": (ws / ".vscode").is_dir(),
                    "script_exists": test_py.is_file(),
                    "returncode": proc2.returncode,
                    "stdout": proc2.stdout.strip(),
                },
            )
        )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/vscode-start-lab/explore-start.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-start.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:VS Code

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip

下载课程关联工程包:VS Code

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2220.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习