{"record":{"id":"7205003f9a49b147","repo":"nautechsystems/nautilus_trader","slug":"failed-to-import-json-e","errorCode":null,"errorMessage":"Failed to import json: {e}","messagePattern":"Failed to import json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/python/data/mod.rs","lineNumber":357,"sourceCode":"    };\n    Ok(custom)\n}\n\n/// Deserializes JSON value to `CustomData` via the data class's `from_json`.\n#[cfg(feature = \"python\")]\nfn py_json_deserialize_custom_data(\n    data_class: &pyo3::Py<pyo3::PyAny>,\n    value: &serde_json::Value,\n) -> Result<std::sync::Arc<dyn crate::data::CustomDataTrait>, anyhow::Error> {\n    use std::sync::Arc;\n\n    use crate::data::PythonCustomDataWrapper;\n\n    pyo3::Python::attach(|py| {\n        let json_str = serde_json::to_string(&value)?;\n        let json_module = py\n            .import(\"json\")\n            .map_err(|e| anyhow::anyhow!(\"Failed to import json: {e}\"))?;\n        let py_dict = json_module\n            .call_method1(\"loads\", (json_str,))\n            .map_err(|e| anyhow::anyhow!(\"Failed to parse JSON: {e}\"))?;\n\n        let instance = data_class\n            .bind(py)\n            .call_method1(\"from_json\", (py_dict,))\n            .map_err(|e| anyhow::anyhow!(\"Failed to call from_json: {e}\"))?;\n\n        let wrapper = PythonCustomDataWrapper::new(py, &instance)\n            .map_err(|e| anyhow::anyhow!(\"Failed to create wrapper: {e}\"))?;\n\n        Ok(Arc::new(wrapper) as Arc<dyn crate::data::CustomDataTrait>)\n    })\n}\n\n/// Encodes `CustomData` items to `RecordBatch` via Python `encode_record_batch_py`.\n#[allow(unsafe_code)]","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/python/data/mod.rs#L339-L375","documentation":"py_json_deserialize_custom_data serializes a custom data value to JSON, then uses Python's json module inside the deserialization callback. If importing the json module fails (broken/limited Python environment, embedded interpreter without stdlib), the error is wrapped as 'Failed to import json: {e}'.","triggerScenarios":"Registering a custom data class via register_custom_data_class and then deserializing a value when the embedded Python interpreter cannot import the stdlib json module — e.g. PYTHONHOME/PYTHONPATH misconfigured in an embedded build, frozen/trimmed Python distributions, or a stdlib shadowed by a local file named json.py.","commonSituations":"Deploying nautilus in a slim container with an incomplete Python installation; embedding the Rust engine with a Python interpreter missing its stdlib; a user module named json.py shadowing the stdlib on sys.path.","solutions":["Fix the embedded Python environment so `import json` works (set PYTHONHOME/PYTHONPATH correctly or ship the full stdlib).","Remove or rename any local json.py module shadowing the stdlib on sys.path.","Verify in the same interpreter: python -c \"import json\"; if it fails, reinstall/repair the Python runtime."],"exampleFix":"// before: interpreter without usable stdlib -> import fails\nlet instance = py_json_deserialize_custom_data(data_class, value)?;\n// after: verify environment at startup\n// $ python -c \"import json\"   # must succeed\n// set PYTHONHOME to the prefix of the Python that ships the stdlib\nstd::env::set_var(\"PYTHONHOME\", \"/usr\");\nlet instance = py_json_deserialize_custom_data(data_class, value)?;","handlingStrategy":"validation","validationCode":"// run once at startup before registering custom data classes\npyo3::Python::attach(|py| {\n    py.import(\"json\").map_err(|e|\n        anyhow!(\"embedded Python cannot import json stdlib: {e}; check PYTHONHOME/PYTHONPATH\"))\n})?;","typeGuard":null,"tryCatchPattern":"match py_json_deserialize_custom_data(data_class, value) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"Failed to import json\") => {\n        // repair embedded interpreter stdlib, then retry once\n        repair_python_env()?;\n        py_json_deserialize_custom_data(data_class, value)?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Verify `python -c \"import json\"` works in the deployment environment.","Set PYTHONHOME/PYTHONPATH correctly for embedded interpreters.","Never shadow the stdlib with a local json.py module."],"tags":["python","json","custom-data","deserialization"],"backgroundTag":"missing-dependency","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}