{"record":{"id":"6e5db157aa9b970e","repo":"datawhalechina/hello-agents","slug":"mx-apikey-export-mx-apikey-your","errorCode":null,"errorMessage":"MX_APIKEY 环境变量未设置，请先设置环境变量：\nexport MX_APIKEY=your_api_key_here\n或者在初始化时传入 api_key 参数","messagePattern":"MX_APIKEY 环境变量未设置，请先设置环境变量：\nexport MX_APIKEY=your_api_key_here\n或者在初始化时传入 api_key 参数","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/lcyting-StockSage-agent/skills/智能选股/mx-xuangu/mx_xuangu.py","lineNumber":129,"sourceCode":"            else:\n                cn_row[cn_name] = str(val)\n        rows.append(cn_row)\n\n    return rows\n\nclass MXSelectStock:\n    \"\"\"妙想智能选股客户端\"\"\"\n    \n    BASE_URL = \"https://mkapi2.dfcfs.com/finskillshub/api/claw/stock-screen\"\n    \n    def __init__(self, api_key: Optional[str] = None):\n        \"\"\"\n        初始化客户端\n        :param api_key: MX API Key，如果不提供则从环境变量 MX_APIKEY 读取\n        \"\"\"\n        self.api_key = api_key or os.getenv(\"MX_APIKEY\")\n        if not self.api_key:\n            raise ValueError(\n                \"MX_APIKEY 环境变量未设置，请先设置环境变量：\\n\"\n                \"export MX_APIKEY=your_api_key_here\\n\"\n                \"或者在初始化时传入 api_key 参数\"\n            )\n    \n    def search(self, query: str) -> Dict[str, Any]:\n        \"\"\"\n        智能选股\n        :param query: 自然语言查询，如 \"今天A股价格大于10元\"\n        :return: API 响应结果\n        \"\"\"\n        headers = {\n            \"Content-Type\": \"application/json\",\n            \"apikey\": self.api_key\n        }\n        data = {\n            \"keyword\": query\n        }","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/skills/智能选股/mx-xuangu/mx_xuangu.py#L111-L147","documentation":"MXSelectStock's constructor raises ValueError when no API key is available: it takes api_key from the argument or the MX_APIKEY environment variable, and neither is set. The key authorizes calls to the MiaoXiang stock-screening endpoint (mkapi2.dfcfs.com), so the client refuses to construct without it.","triggerScenarios":"MXSelectStock() with no api_key argument while MX_APIKEY is unset; .env present in the project root but this class reads only os.getenv (it does not parse .env itself), so a .env alone does not help unless loaded.","commonSituations":"Assuming .env is read automatically (unlike mx_zixuan which hand-parses .env, this class only reads env); running under a launcher that doesn't source .env; key stored under a different variable name.","solutions":["export MX_APIKEY=your_key in the shell/environment that runs the code, or pass api_key=\"...\" to MXSelectStock(api_key=...).","If the key lives in .env, call load_dotenv() before constructing the client.","In Docker/compose, add MX_APIKEY to environment: or env_file:.","Standardize on one env-loading mechanism across skills to avoid the .env-vs-env mismatch."],"exampleFix":"# before\nclient = MXSelectStock()  # ValueError: MX_APIKEY not set\n\n# after\nfrom dotenv import load_dotenv\nload_dotenv()  # reads .env into os.environ\nclient = MXSelectStock()  # or MXSelectStock(api_key=os.environ[\"MX_APIKEY\"])","handlingStrategy":"validation","validationCode":"from dotenv import load_dotenv\nload_dotenv()  # this class reads only os.getenv, not .env directly\nif not os.getenv(\"MX_APIKEY\"):\n    raise SystemExit(\"MX_APIKEY not set; export it or pass api_key= to MXSelectStock\")\nclient = MXSelectStock()","typeGuard":"def mx_key_present(api_key: str | None = None) -> bool:\n    return bool(api_key or os.getenv(\"MX_APIKEY\"))","tryCatchPattern":"try:\n    client = MXSelectStock()\nexcept ValueError as e:\n    if \"MX_APIKEY\" in str(e):\n        client = MXSelectStock(api_key=os.environ[\"MX_APIKEY_FILE_CONTENTS\"])  # explicit injection\n    else:\n        raise","preventionTips":["Remember MXSelectStock reads env only — a .env file without load_dotenv() is not enough.","Pass api_key explicitly when the caller already holds the credential.","Add MX_APIKEY to container/compose env configuration."],"tags":["env-vars","api-key","configuration","python"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}