hsliuping/TradingAgents-CN · critical · ImportError
pymongo is not installed. Please install it with: pip instal
Error message
pymongo is not installed. Please install it with: pip install pymongo
What it means
Raised by MongoDBStorage.__init__ when the optional pymongo dependency is not installed (MONGODB_AVAILABLE is False). The module imports pymongo in a try/except at import time and the storage class guards instantiation.
Source
Thrown at tradingagents/config/mongodb_storage.py:33
from tradingagents.utils.logging_manager import get_logger
from tradingagents.config.runtime_settings import get_timezone_name
logger = get_logger('agents')
try:
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
MONGODB_AVAILABLE = True
except ImportError:
MONGODB_AVAILABLE = False
MongoClient = None
class MongoDBStorage:
"""MongoDB存储适配器"""
def __init__(self, connection_string: str = None, database_name: str = "tradingagents"):
if not MONGODB_AVAILABLE:
raise ImportError("pymongo is not installed. Please install it with: pip install pymongo")
# 修复硬编码问题 - 如果没有提供连接字符串且环境变量也未设置,则抛出错误
self.connection_string = connection_string or os.getenv("MONGODB_CONNECTION_STRING")
if not self.connection_string:
raise ValueError(
"MongoDB连接字符串未配置。请通过以下方式之一进行配置:\n"
"1. 设置环境变量 MONGODB_CONNECTION_STRING\n"
"2. 在初始化时传入 connection_string 参数\n"
"例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/"
)
self.database_name = database_name
self.collection_name = "token_usage"
self.client = None
self.db = None
self.collection = None
self._connected = FalseView on GitHub (pinned to 74783e8817)
Solutions
- pip install pymongo
- Or install the project with the mongodb extra if provided (pip install -e '.[mongodb]')
- Verify: python -c "import pymongo; print(pymongo.version)"
- If MongoDB is optional in your flow, catch ImportError and fall back to another storage backend
Example fix
# before
storage = MongoDBStorage() # ImportError
# after
try:
from tradingagents.config.mongodb_storage import MongoDBStorage
storage = MongoDBStorage()
except ImportError:
storage = None # or fallback backend Defensive patterns
Strategy: try-catch
Validate before calling
try:
import pymongo # noqa
MONGODB_AVAILABLE = True
except ImportError:
MONGODB_AVAILABLE = False
if not MONGODB_AVAILABLE:
# skip mongo-backed path
pass Try / catch
try:
storage = MongoDBStorage(connection_string=cs)
except ImportError as e:
if 'pymongo' in str(e):
storage = LocalStorage() # fallback
else:
raise Prevention
- Declare pymongo in requirements or extras
- Dockerfile: pip install pymongo
- Guard optional backends with availability flags like the library does
When it happens
Trigger: Constructing MongoDBStorage(...) in an environment where `import pymongo` failed, e.g. pip install tradingagents without extras, or a venv missing pymongo.
Common situations: Base install without the mongo extra, production image slimmed down, dependency conflicts removing pymongo during a pip resolver pass.
Related errors
- MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\n例如: MONGO
- MongoDB连接字符串未配置。请通过以下方式之一进行配置:\n1. 设置环境变量 MONGODB_CONNECTION
- 无法获取股票 {symbol} 的财务数据。已尝试所有数据源(MongoDB、AKShare、Tushare)均失败。
- name
- Redis连接配置未完整设置。请设置以下环境变量之一:\n1. REDIS_CONNECTION_STRING=redi
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/19b1270eac044979.
Report an issue: GitHub.