"""Offline Python verification lab for PyCharm Chapter 05 (first-python-project)."""
import json
import subprocess
import sys
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
VERSION = "CPython 3.12.14 offline PyCharm workflow verifier (desktop IDE not launched)"
def case_create_first_project_and_script():
with tempfile.TemporaryDirectory() as tmp:
proj = Path(tmp) / "hello_pycharm_project"
(proj / ".idea").mkdir(parents=True)
script = proj / "hello_demo.py"
script.write_text('print("Hello, PyCharm!")\n', encoding="utf-8")
proc = subprocess.run(
[sys.executable, "-I", "-B", "-X", "utf8", str(script)],
cwd=proj,
capture_output=True,
encoding="utf-8",
timeout=15,
)
expected = {
"project_dir_exists": True,
"script_name": "hello_demo.py",
"returncode": 0,
"stdout": "Hello, PyCharm!\n",
}
actual = {
"project_dir_exists": (proj / ".idea").is_dir(),
"script_name": script.name,
"returncode": proc.returncode,
"stdout": proc.stdout,
}
return {
"name": "create_first_project_and_script",
"summary": "在临时目录创建首个 Python 项目结构与入口脚本并实际执行,校验标准输出与退出码 0",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_interpreter_xml_binding():
unconfigured = '<project version="4"><component name="ProjectRootManager" /></project>'
configured = (
'<project version="4">'
'<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (hello_pycharm_project)" project-jdk-type="Python SDK" />'
"</project>"
)
def inspect_jdk(xml_text):
node = ET.fromstring(xml_text).find("./component[@name='ProjectRootManager']")
return node.attrib.get("project-jdk-name") if node is not None else None
expected = {
"before_missing": True,
"after_jdk": "Python 3.12 (hello_pycharm_project)",
}
actual = {
"before_missing": inspect_jdk(unconfigured) is None,
"after_jdk": inspect_jdk(configured),
}
return {
"name": "interpreter_xml_binding",
"summary": "模拟 .idea/misc.xml 从未配置解释器到绑定 Python 3.12 解释器的状态变化",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_third_party_package_install_spec():
settings_paths = {
"Windows/Linux": "File -> Settings -> Project -> Python Interpreter -> +",
"macOS": "PyCharm -> Preferences/Settings -> Project -> Python Interpreter -> +",
}
expected = {
"platform_count": 2,
"uses_plus_button": True,
}
actual = {
"platform_count": len(settings_paths),
"uses_plus_button": all(v.endswith("+") for v in settings_paths.values()),
}
return {
"name": "third_party_package_install_spec",
"summary": "核对 Windows/Linux 与 macOS 在项目解释器界面添加第三方库的标准入口路径",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_create_first_project_and_script(),
case_interpreter_xml_binding(),
case_third_party_package_install_spec(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。
入口文件:projects/pycharm-first-python-project-lab/explore-first-python-project.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-first-python-project.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2245.zip
课程关联工程包(登记关联17章) · 含当前章节或实例 · mbskycom202612a2240.zip