datawhalechina/hello-agents · critical · RuntimeError
MX_APIKEY 未配置
Error message
MX_APIKEY 未配置
What it means
The mx_zixuan helper raises RuntimeError("MX_APIKEY not configured") when key resolution fails entirely: it checks the environment, then hand-parses .env files for a `MX_APIKEY=` line (splitting on the first '='), and only raises after both come up empty. It also prints stderr diagnostics naming where to look before raising.
Source
Thrown at Co-creation-projects/lcyting-StockSage-agent/skills/自选股管理/mx-zixuan/mx_zixuan.py:50
# 尝试从.env文件读取
env_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), ".env")
if os.path.exists(env_file):
try:
with open(env_file, "r") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
if key.strip() == "MX_APIKEY":
apikey = value.strip()
break
except Exception as e:
print(f"⚠️ 读取.env文件失败: {e}", file=sys.stderr)
if not apikey:
print("❌ 未找到MX_APIKEY,请设置环境变量:", file=sys.stderr)
print(" export MX_APIKEY=your_apikey", file=sys.stderr)
raise RuntimeError("MX_APIKEY 未配置")
return apikey
def query_self_select(apikey: str) -> Dict:
"""查询自选股列表"""
headers = {
"Content-Type": "application/json",
"apikey": apikey
}
response = requests.post(QUERY_URL, headers=headers, json={}, timeout=30)
response.raise_for_status()
return response.json()
def manage_self_select(apikey: str, query: str) -> Dict:
"""添加或删除自选股"""
headers = {
"Content-Type": "application/json",View on GitHub (pinned to 606a07d341)
Solutions
- export MX_APIKEY=your_key, or ensure a .env file containing `MX_APIKEY=...` exists in the location the helper searches.
- Check the stderr output — it prints the exact remediation and any .env read failure reason.
- Verify the .env line format: key exactly `MX_APIKEY`, one `=`, value on the same line; comment lines starting with # are skipped.
- Pass the key explicitly if the API exposes an apikey parameter (the query functions take apikey as an argument).
Example fix
# before
apikey = get_apikey() # RuntimeError if neither env nor .env has the key
# after
import os
os.environ.setdefault("MX_APIKEY", "your_key_here") # or fix .env: MX_APIKEY=your_key
apikey = get_apikey() Defensive patterns
Strategy: validation
Validate before calling
import os, sys
key = os.getenv("MX_APIKEY")
if not key:
# mirror the helper's .env lookup before calling it
for path in (".env", os.path.expanduser("~/.env")):
try:
with open(path) as f:
for line in f:
line = line.strip()
if line.startswith("MX_APIKEY="):
key = line.split("=", 1)[1].strip()
break
except FileNotFoundError:
continue
if not key:
raise SystemExit("MX_APIKEY not configured; set env var or .env entry") Type guard
def mx_apikey_resolvable() -> bool:
if os.getenv("MX_APIKEY"):
return True
try:
with open(".env") as f:
return any(l.strip().startswith("MX_APIKEY=") for l in f)
except OSError:
return False Try / catch
try:
apikey = get_apikey()
except RuntimeError as e:
if "MX_APIKEY" in str(e):
raise SystemExit("configure MX_APIKEY (export or .env line 'MX_APIKEY=...')") from e
raise Prevention
- Watch stderr — the helper prints exactly which sources it checked before raising.
- Keep the .env line format exact: 'MX_APIKEY=value' with no spaces around '='.
- Prefer setting the env var in the launching process over relying on .env parsing.
When it happens
Trigger: No MX_APIKEY in os.environ and no .env (or .env without an MX_APIKEY= line) reachable from the working directory; .env line malformed so the parser misses it (no '=', or key written as MXAPIKEY); read failure on .env is only warned about, then the RuntimeError follows.
Common situations: Running the skill from a directory where no .env exists along the helper's search paths; malformed .env lines (missing '=', key written as MXAPIKEY, inline comments); CI/cron processes without the env var exported; key stored under a different variable name.
Related errors
- 未配置 AMiner API Key。请前往 https://open.aminer.cn/ 注册获取,然后在 .env
- MX_APIKEY 环境变量未设置,请先设置环境变量: export MX_APIKEY=your_api_key_he
- TMDB 未配置:请在 .env 设置 TMDB_ACCESS_TOKEN 或 TMDB_API_KEY
- TAVILY_API_KEY is required for TavilySearchTool
- Missing required environment variables: {missing_env_vars}.
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/6d233eeb2a177120.
Report an issue: GitHub.