Use this when Python automation fails and you need the traceback, venv and logging checks.
Python Automation Troubleshooting
Troubleshoot Python automation by reading the full traceback, checking the active virtual environment and package versions (pip list), adding logging, and testing pieces in the REPL or with a debugger like pdb.
1. Check the venv and dependency versions first.
2. Read the full traceback.
3. Reproduce with same inputs.
4. Add logging around failure.
5. Write a regression test.
error → traceback → environment → fix → test
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
python -c "import ast,pathlib; ast.parse(pathlib.Path('<script.py>').read_text())"Primary Commands
Configuration and operational commands for this Topic.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Command read-back and what it shows Verify boundary
Known-good is the script parses, required imports load, the intended code path exits successfully and the original external/postcondition check confirms the expected state; broken state is syntax/import/runtime exceptions, wrong interpreter/dependency context or zero-error execution followed by incorrect/missing downstream state. AST parsing shows Python syntax structure only, but it does not show imports, runtime branches, credentials, API behavior or postconditions.
Step 2 Common mistakes Narrow the fault
- Debugging without logging the request/response.
Step 3 Observation-driven branch and next-check logic Narrow the fault
If AST parsing fails, fix the reported syntax location first; if parsing passes but imports/startup fail, inspect interpreter/venv/dependencies; if startup succeeds but runtime task fails, capture the first exception/response and compare inputs/environment with a known-good run.
- Read the full traceback from the bottom up.
- Check the active virtual environment and versions.
- Add logging around the failing code.
python3 -X dev script.py— dev mode warningspython3 -m pdb script.py— debugpip check— dependency conflicts
ReferenceHow it works1 section · depth on demand
How it works
Python Automation Troubleshooting is governed by Python source parsing, import/module resolution, runtime exceptions and the external systems called by the script. Python first parses source into an AST, then resolves imports/names and executes code paths whose exceptions may originate in local logic or external libraries/APIs; syntax validity is only the earliest boundary. Virtual environment, package versions, filesystem/permissions, environment variables, credentials and remote APIs remain separate things to check. The important state separation is that a script can parse successfully while imports fail or runtime logic is wrong, and a successful function return does not show an external asynchronous action completed.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Troubleshooting Python automation follows the traceback: the last lines show the exception type and message, the lines above show where it was raised. Most failures fall into a few categories.
| Error | Usual cause |
|---|---|
ModuleNotFoundError | Wrong interpreter/venv, package not installed |
KeyError / TypeError: 'NoneType' | API returned different structure or null field |
ConnectionError / Timeout | Network, DNS, proxy, firewall |
SSLError: certificate verify failed | Missing internal CA in trust store (REQUESTS_CA_BUNDLE) |
UnicodeDecodeError | Wrong file encoding |
Netmiko ReadTimeout | Prompt not matched, slow command |
python -X dev -m pdb script.py # debugger
python -c 'import sys; print(sys.executable)'
Add logging at DEBUG level around external calls, and reproduce with a single target before running against many.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > Python Automation Troubleshooting. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/ · confidence 94%