plugins/python3-development/skills/stdlib-scripting/SKILL.md
LAST RESORT for stdlib-only Python 3.11+ scripts in CONFIRMED restricted environments (airgapped systems, no uv, no internet access). Creates portable dependency-free scripts using argparse, logging, config management (JSON/TOML/INI), and cross-platform patterns. Use ONLY when environment restrictions prevent Typer+Rich with PEP 723. Triggers on "stdlib-only script", "no dependencies", "airgapped", "restricted environment", "portable script no network". For standard CLI development, use python-cli-architect with Typer+Rich instead.
npx skillsauth add jamie-bitflight/claude_skills stdlib-scriptingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill is a LAST RESORT. Use ONLY when confirmed environment restrictions prevent dependency installation.
flowchart TD
Start([Need Python CLI script]) --> Q1{Environment allows network access<br>for dependency installation?}
Q1 -->|Yes — standard environment| UseTyper[Use python-cli-architect with Typer+Rich<br>Less code, better UX, well-tested libraries]
Q1 -->|No — restricted environment| Q2{Restriction type confirmed?}
Q2 -->|No — assumption only| Stop[STOP — Verify with user first<br>Do not assume restrictions]
Q2 -->|Yes — airgapped/no internet/no uv| StdLib[Use stdlib-scripting<br>Maximum portability, basic UX]
UseTyper --> Why1[Why: PEP 723 + uv handles dependencies<br>Simpler code, richer features]
StdLib --> Why2[Why: Runs anywhere Python 3.11+ installed<br>More code to write and maintain]
See ../python3-development/SKILL.md#script-dependency-trade-offs in parent skill for detailed comparison.
Create portable, dependency-free Python 3.11+ scripts that use only standard library at runtime. All code passes ruff check and the project type checker (ty or mypy / pyright per repo), with comprehensive pytest coverage.
Fundamental principle: Patterns are tools, not goals. Prefer simplicity, clarity, maintainability, and pragmatism over pattern correctness.
Required report:
REFACTORING IMPACT ANALYSIS:
- Line count: X -> Y (∆ Z)
- Functions affected: N
- Patterns applied: [...]
- Complexity reduction: [...]
- Rejected changes: [reasons]
Automated:
python -m py_compile <file>
grep -E "from typing import .*(Dict|List|Set|Tuple|Optional|Union)\b" <file>
ruff check <file>
# Type check: match the host project — ty is default in this repo (`uv run ty check <file>`);
# use `mypy --strict <file>` when the project standardizes on mypy or uv is unavailable.
Checklist:
Validation output (required):
VALIDATION REPORT:
✅ Syntax check: PASSED
✅ Forbidden patterns: NONE FOUND
✅ Native type hints: VERIFIED (X instances)
✅ Ruff: PASSED
✅ Type check (ty or mypy): PASSED
✅ Checklist items: COMPLETED
ruff check and type checking clean (ty or mypy --strict per project)from shutil import which
import os, subprocess
def run_privileged_command(cmd: str, args: list[str]) -> subprocess.CompletedProcess[str]:
if os.name != "posix":
return subprocess.run([cmd, *args], capture_output=True, text=True)
if os.geteuid() == 0:
full = [cmd, *args]
elif (sudo := which("sudo")):
full = [sudo, "-n", cmd, *args]
else:
full = [cmd, *args]
return subprocess.run(full, capture_output=True, text=True, check=False)
import argparse
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Tool description",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
return parser
import logging
from pathlib import Path
def setup_logging(level: str = "INFO", log_file: Path | None = None) -> None:
handlers: list[logging.Handler] = [logging.StreamHandler()]
if log_file:
handlers.append(logging.FileHandler(log_file))
logging.basicConfig(
level=getattr(logging, level.upper(), logging.INFO),
format="%(asctime)s %(name)s %(levelname)s %(message)s",
handlers=handlers,
)
import json, configparser, tomllib
from pathlib import Path
from typing import Any
def load_config(path: Path) -> dict[str, Any]:
ext = path.suffix.lower()
data = path.read_text()
if ext == ".json":
return json.loads(data)
if ext == ".toml":
return tomllib.loads(data)
if ext in (".ini", ".cfg"):
p = configparser.ConfigParser()
p.read_string(data)
return {sec: dict(p[sec]) for sec in p.sections()}
raise ValueError(f"Unsupported config format: {ext}")
import logging, sys
class ScriptError(Exception):
"""Base error for script-related failures."""
def handle_error(err: Exception, logger: logging.Logger) -> None:
logger.error(f"Error: {err}")
if logger.isEnabledFor(logging.DEBUG):
logger.exception("Traceback")
sys.exit(1)
import asyncio
from collections.abc import Coroutine
from typing import Any
async def run_async(tasks: list[Coroutine[Any, Any, Any]]) -> list[Any]:
return await asyncio.gather(*tasks, return_exceptions=True)
When PEP 723 single-file approach is insufficient:
project_name/
├── main.py
├── core/
│ ├── __init__.py
│ ├── config.py
│ ├── logging_setup.py
│ └── exceptions.py
├── utils/
│ ├── __init__.py
│ └── helpers.py
├── tests/
│ ├── __init__.py
│ ├── test_main.py
│ └── test_core.py
├── config.example.toml
└── README.md
Pytest with coverage of normal, edge, and error cases.
import json
from pathlib import Path
from module import load_config
def test_load_json(tmp_path: Path) -> None:
path = tmp_path / "c.json"
path.write_text(json.dumps({"k": "v"}))
result = load_config(path)
assert result == {"k": "v"}
For ALL Python scripts you create or modify, validate the shebang:
/python3-development:shebangpython <file_path>
This ensures stdlib-only scripts use #!/usr/bin/env python3 without PEP 723 metadata (nothing to declare).
Typing guidance, protocols, type aliases, and advanced patterns:
references/typing-strategy.md - Protocol vs TypeVar vs ParamSpec, abstract collections, Any boundaries, JSON handlingreferences/command-execution.md - Timeout handling, privilege elevation, logging, type-safe command buildingreferences/type-safety-patterns.md - Overloads, protocols, type narrowing, linter settingsdevelopment
When an application needs to store config, data, cache, or state files. When designing where user-specific files should live. When code writes to ~/.appname or hardcoded home paths. When implementing cross-platform file storage with platformdirs.
testing
Enforce mandatory pre-action verification checkpoints to prevent pattern-matching from overriding explicit reasoning. Use this skill when about to execute implementation actions (Bash, Write, Edit) to verify hypothesis-action alignment. Blocks execution when hypothesis unverified or action targets different system than hypothesis identified. Critical for preventing cognitive dissonance where correct diagnosis leads to wrong implementation.
tools
Reference guide for the Twelve-Factor App methodology — 15 principles (12 original + 3 modern extensions) for building portable, resilient, cloud-native applications. Use when evaluating application architecture, designing cloud-native services, reviewing codebases for methodology compliance, advising on configuration, scaling, observability, security, and deployment patterns. Incorporates the 2025 open-source community evolution and cloud-native reinterpretations of each factor.
tools
Converts user-facing documentation (how-to guides, tutorials, API references, examples) in any format — Markdown, PDF, DOCX, PPTX, XLSX, AsciiDoc, RST, HTML, Jupyter notebooks, man pages, TOML/YAML/JSON configs, and plain text — into Claude Code skill directories with SKILL.md plus thematically grouped references/*.md files. Use when given a docs directory or mixed-format documentation to transform into an AI skill. Uses MCP file-reader server for binary formats.