{"record":{"id":"556ce043c2be3ad7","repo":"langchain-ai/langchain","slug":"could-not-import-module-name-python-package-ple","errorCode":null,"errorMessage":"Could not import {module_name} python package. Please install it with `pip install {pip_name}`.","messagePattern":"Could not import (.+?) python package\\. Please install it with `pip install (.+?)`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/utils.py","lineNumber":142,"sourceCode":"        module_name: The name of the module to import.\n        pip_name: The name of the module to install with pip.\n        package: The package to import the module from.\n\n    Returns:\n        The imported module.\n\n    Raises:\n        ImportError: If the module is not installed.\n    \"\"\"\n    try:\n        module = importlib.import_module(module_name, package)\n    except (ImportError, ModuleNotFoundError) as e:\n        pip_name = pip_name or module_name.split(\".\", maxsplit=1)[0].replace(\"_\", \"-\")\n        msg = (\n            f\"Could not import {module_name} python package. \"\n            f\"Please install it with `pip install {pip_name}`.\"\n        )\n        raise ImportError(msg) from e\n    return module\n\n\ndef check_package_version(\n    package: str,\n    lt_version: str | None = None,\n    lte_version: str | None = None,\n    gt_version: str | None = None,\n    gte_version: str | None = None,\n) -> None:\n    \"\"\"Check the version of a package.\n\n    Args:\n        package: The name of the package.\n        lt_version: The version must be less than this.\n        lte_version: The version must be less than or equal to this.\n        gt_version: The version must be greater than this.\n        gte_version: The version must be greater than or equal to this.","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/utils.py#L124-L160","documentation":"Raised by `import_module` in `langchain_core.utils.utils` when `importlib.import_module` fails: the requested module (an optional dependency) is not installed in the current environment. The message tells you the exact `pip install <package>` command to run, mapping underscores to hyphens (e.g. `langchain_openai` → `pip install langchain-openai`).","triggerScenarios":"Using an integration or utility that lazily imports an optional package — e.g. a chat model trying to import `langchain_openai`, `aiohttp`, `tiktoken` — while that package is absent from the venv. Also raised when the package is installed but its own imports fail inside a broken environment (the original ImportError is chained).","commonSituations":"Installing only `langchain-core` and using partner integrations; multiple virtualenvs/interpreters (package installed in one, app runs in another); Docker images trimmed of optional deps; version conflicts where the package's transitive import fails; typos in the module name passed by custom code.","solutions":["Run the exact command from the message: `pip install <pip_name>` (or `uv add`/`uv sync --group ...` in this monorepo).","If it is already installed, verify you are running the same interpreter/venv: `python -c \"import sys; print(sys.executable)\"`.","In Docker/CI, add the optional dependency to the image/environment definition.","Check the chained exception (`raise ... from e`) for a nested ImportError indicating a broken install — reinstall the package."],"exampleFix":"# before\n# ImportError: Could not import langchain_openai python package. Please install it with `pip install langchain-openai`.\nfrom langchain_openai import ChatOpenAI\n\n# after (terminal)\n# pip install langchain-openai\nfrom langchain_openai import ChatOpenAI","handlingStrategy":"try-catch","validationCode":"import importlib.util\n\ndef module_available(module_name: str) -> bool:\n    return importlib.util.find_spec(module_name) is not None\n\nif not module_available(\"langchain_openai\"):\n    raise RuntimeError(\"this feature requires: pip install langchain-openai\")","typeGuard":null,"tryCatchPattern":"try:\n    from langchain_core.utils.utils import import_module\n    mod = import_module(\"langchain_openai\")\nexcept ImportError as e:\n    raise RuntimeError(\"optional dependency missing; run: pip install langchain-openai\") from e","preventionTips":["Declare optional integrations as explicit dependencies in pyproject/requirements rather than relying on lazy imports.","Pin the interpreter/venv in scripts (`uv run`, absolute python path) to avoid cross-env mixups.","Smoke-test imports in CI and Docker builds."],"tags":["dependencies","import","environment"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}