# Checking for Interactive Shell in a Python Script (pythontutorials.net) ## Metadata | Field | Value | |---|---| | Institute | Molodetz Institute — Research & Retrieval Division | | Subject | shell-multiplexer-auto-attach | | Document type | Source Dossier | | Tier | 1 | | Source title | How to Check if a Shell is Interactive in a Python Script: Preferred Methods Explained | | Source URL | https://www.pythontutorials.net/blog/checking-for-interactive-shell-in-a-python-script/ | | Source publisher / author | pythontutorials.net | | Source type | blog post | | Source date (published) | Unknown — not stated | | Date researched | 2026-10-09 | | Researched by | tai (agent), model: Claude Sonnet 5, on behalf of retoor | | Research method | web search synthesis | | Confidence | Medium | | Status | Active | ## Summary Suggests checking whether `PS1` exists in a Python script's environment as a proxy for shell interactivity — flagged independently (not by this source, but by general knowledge noted in the search synthesis) as unreliable, since `PS1` is normally a shell variable that is not exported to child processes. ## Key findings - `PS1`, unless explicitly `export`ed, is invisible to any child process (including a Python script, or tai itself if written in Python) inspecting its own environment — `os.environ.get("PS1")` will typically be empty even in a genuinely interactive parent shell. - This caveat is a real correctness bug if a tai component written in Python ever tried to re-derive "is the parent shell interactive" from its own `os.environ`. ## Pros / strengths - Surfaces a specific, concrete correctness trap for exactly the kind of cross-language (bash-to-Python) interactivity check the proposal would otherwise be tempted to write. ## Cons / limitations / caveats - The source itself suggests the `PS1`-in-environment check as a *method*; the caveat about its unreliability from a child process comes from general knowledge noted alongside the search synthesis, not from the source's own text — treat the caveat as independently well-established (PS1 export behavior is standard shell semantics), but attribute it honestly as outside this specific article. ## Relevance to tai A hard "don't do this" for the proposal: any Python-side component of tai must never try to detect shell interactivity by reading `PS1` from `os.environ` — it won't be there even when the parent genuinely is interactive. Detection has to happen in the shell (`.bashrc`, via `$-`) and be passed to tai explicitly (an env var, an argument), not re-derived downstream. ## Changelog | Date | Change | |---|---| | 2026-10-09 | Initial dossier created. |