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

检查独立解释器、命令发现与固定版数据目录

PYTHONWindows、CPython3.12.14创建的临时venv及固定Hermes路径模块;完整Hermes未安装,官方托管运行时另有要求。environment_check.py
environment_check.py69 行
"""Inspect a fresh Python environment and a pinned Hermes directory resolver; do not install Hermes."""
import argparse
import importlib.util
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
import venv

def resolver():
    path=Path(__file__).with_name("vendor")/"hermes_constants.py"
    spec=importlib.util.spec_from_file_location("owned_fixed_hermes_paths",path)
    module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
    return module

def probe():
    return dict(prefix=sys.prefix,base_prefix=sys.base_prefix,version=list(sys.version_info[:3]),
        home=str(resolver().get_hermes_home()),hermes_command=shutil.which("hermes"),
        user_site_disabled=sys.flags.no_user_site==1)

def exercise(root):
    root=Path(root).resolve();environment=root/"fresh-python"
    venv.EnvBuilder(with_pip=False,symlinks=False).create(environment)
    binary=environment/("Scripts/python.exe" if os.name=="nt" else "bin/python")
    env={key:os.environ[key] for key in ("SystemRoot","WINDIR","TEMP","TMP") if key in os.environ}
    env.update(PATH=str(binary.parent),LOCALAPPDATA=str(root/"os-local"),PYTHONIOENCODING="utf-8")
    entry=Path(__file__).resolve()
    def child(extra):
        completed=subprocess.run([str(binary),"-I","-B","-X","utf8",str(entry),"--probe"],
            cwd=root,env=dict(env,**extra),capture_output=True,encoding="utf8",timeout=15,check=True)
        return json.loads(completed.stdout)
    default=child({});custom=root/"independent-data";configured=child(dict(HERMES_HOME=str(custom)))
    missing=subprocess.run([str(binary),"-I","-B","-m","pip","--version"],cwd=root,env=env,
        capture_output=True,encoding="utf8",timeout=10)
    facts=json.loads(entry.with_name("requirements-evidence.json").read_text(encoding="utf8"))
    cases=[]
    def check(name,inputs,operation,expected,actual):
        assert expected==actual,(name,expected,actual)
        cases.append(dict(name=name,input=inputs,operation=operation,expected=expected,actual=actual))
    check("metadata",dict(commit=facts["commit"]),"read fixed package range and managed Python lock version separately",
        [">=3.11,<3.15","3.14.9+20261021"],[facts["requires_python"],facts["managed_python"]])
    check("fresh_venv",dict(with_pip=False),"create and run a real isolated Python interpreter without activation",
        [True,True,True],[Path(default["prefix"])==environment,default["prefix"]!=default["base_prefix"],
            default["version"]==list(sys.version_info[:3])])
    check("pip_absent",dict(with_pip=False),"run the fresh interpreter's pip module and observe it is absent",
        [True,True],[missing.returncode!=0,"No module named pip" in missing.stderr])
    check("command_discovery",dict(PATH="only the owned environment's Scripts/bin directory"),
        "resolve hermes in a fresh environment where it was never installed",
        [None,True],[default["hermes_command"],default["user_site_disabled"]])
    check("windows_default",dict(LOCALAPPDATA="owned temporary directory",HERMES_HOME="unset"),
        "use the pinned upstream resolver under native Windows",str(root/"os-local"/"hermes"),default["home"])
    check("explicit_home",dict(HERMES_HOME="owned independent-data"),"resolve the explicitly selected data directory",
        str(custom),configured["home"])
    check("directory_only",dict(no_installer=True),"confirm path resolution alone did not create data or install an application",
        [False,False],[custom.exists(),(root/"os-local"/"hermes").exists()])
    return dict(cases=cases,python_version=default["version"],requirements=facts,
        scope="fresh CPython environment and native upstream path helper only; no Hermes installer, managed Python download or application launch")

if __name__=="__main__":
    parser=argparse.ArgumentParser(description=__doc__);parser.add_argument("--probe",action="store_true")
    args=parser.parse_args()
    if args.probe:result=probe()
    else:
        with TemporaryDirectory(prefix="mbsky-hermes-environment-") as root:result=exercise(root)
    print(json.dumps(result,ensure_ascii=False))
本例需在 Windows、CPython3.12.14创建的临时venv及固定Hermes路径模块;完整Hermes未安装,官方托管运行时另有要求。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、CPython3.12.14创建的临时venv及固定Hermes路径模块;完整Hermes未安装,官方托管运行时另有要求。

入口文件:projects/hermes-install/environment_check.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 environment_check.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、CPython3.12.14创建的临时venv及固定Hermes路径模块;完整Hermes未安装,官方托管运行时另有要求。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Hermes Agent

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

下载课程关联工程包:Hermes Agent

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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