Skip to content

Commit 0b2c3a6

Browse files
jwesleyeclaude
andcommitted
fix: suppress stderr during parent package loading
Fixes "No module named 'agents'" error shown during startup when using fully qualified agent paths that include intermediate package directories. Issue: When loading agents from paths like /path/agents/my_agent.py, the loader tries to import parent packages. If those __init__.py files have import errors, Python prints error messages to stderr even though the errors are caught and handled. Solution: - Added contextlib.redirect_stderr() to suppress error output during parent package initialization - Errors are still caught and logged at DEBUG level for troubleshooting - Agent loading still works correctly (errors are non-critical) Impact: - Cleaner startup experience with no confusing error messages - Agent functionality unchanged - All 161 tests passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d7cac92 commit 0b2c3a6

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/basic_agent_chat_loop/components/agent_loader.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
Handles dynamic loading of agent modules and extraction of agent metadata.
55
"""
66

7+
import contextlib
78
import importlib.util
9+
import io
810
import logging
911
import os
1012
import sys
@@ -115,12 +117,17 @@ def _ensure_package_loaded(package_root: Path, package_name: str) -> None:
115117
# Register in sys.modules BEFORE executing
116118
sys.modules[pkg] = pkg_module
117119

120+
# Suppress stderr during package init - parent packages in agent paths
121+
# often have import errors that are non-critical (agent still works)
118122
try:
119-
spec.loader.exec_module(pkg_module)
123+
with contextlib.redirect_stderr(io.StringIO()):
124+
spec.loader.exec_module(pkg_module)
120125
except Exception as e:
121-
# If package init fails, still keep it registered
122-
# (might just have imports that will work later)
123-
logger.debug(f"Package {pkg} __init__.py had errors: {e}")
126+
# If package init fails, still keep it registered (might just have
127+
# imports that will work later). This commonly happens with
128+
# intermediate directories in agent paths (e.g., /path/agents/my_agent.py)
129+
if logger.isEnabledFor(logging.DEBUG):
130+
logger.debug(f"Package {pkg} __init__.py initialization failed: {type(e).__name__}")
124131
pass
125132

126133

0 commit comments

Comments
 (0)