Fix ModuleNotFoundError: No module named langchain

Quick Answer

Run python -m pip install -U langchain from the same environment that runs the failing program, then verify sys.executable, langchain.__file__, and the import path. In notebooks or IDEs, select the kernel or interpreter where the package was installed.

LangChain installation visual showing Python, the active environment, and provider integrations
Install LangChain into the same interpreter that runs the application, then verify the import path and provider package.

The ModuleNotFoundError: No module named langchain error means the Python interpreter running your script cannot import the LangChain package from its current environment. The package may not be installed, it may be installed into a different virtual environment, your Python version may be too old for current LangChain releases, or your code may still use imports from an older LangChain layout.

The most reliable fix is to check the interpreter first, install LangChain with that same interpreter, then add the provider integration package your project uses. Current LangChain documentation lists the base install as pip install -U langchain, while provider-specific integrations such as OpenAI, Anthropic, or community loaders are documented separately in the provider integrations overview.

Quick diagnosis

Run the import test from the same terminal, notebook kernel, cron command, web worker, or IDE configuration that raises the error. Do not only test your global Python install, because this error is usually caused by using one interpreter to install packages and another interpreter to run the program.

import langchain

print(langchain.__version__)
print(langchain.__file__)

If that snippet prints a version and a package path, LangChain is installed in the active environment and your problem is probably an outdated import path or a different runtime configuration. If it raises the same ModuleNotFoundError, install the package with the active interpreter instead of guessing which pip executable your shell finds first.

Python Pool infographic separating the LangChain distribution, Python import, integrations, and environment
Package identity: Python Pool infographic separating the LangChain distribution, Python import, integrations, and environment.

Install LangChain in the active environment

Use python -m pip or sys.executable -m pip so the installation targets the interpreter that runs your code. This avoids a common mismatch where pip points to a system Python, but your application runs from a virtual environment, Conda environment, notebook kernel, Docker image, or production service user.

import subprocess
import sys

subprocess.check_call([
    sys.executable,
    "-m",
    "pip",
    "install",
    "-U",
    "langchain",
])

After installation, restart the process that runs your code. For notebooks, restart the kernel. For a web app or background worker, restart the service. Python does not always see newly installed packages in a long-running process until that process starts again.

Check the Python version

Current LangChain versions require a modern Python runtime. The official install page currently states Python 3.10 or newer, so an older interpreter can leave you with failed installs, incompatible dependency resolution, or a package that exists in one environment but not in the environment where your script runs. If you manage several Python versions, verify the version from inside the same runtime that imports LangChain.

import sys

required = (3, 10)
current = sys.version_info[:3]

if current < required:
    raise RuntimeError(
        f"LangChain requires Python {required[0]}.{required[1]}+; "
        f"current interpreter is {current[0]}.{current[1]}.{current[2]}"
    )

print(f"Python version is OK: {current[0]}.{current[1]}.{current[2]}")

If the version is too old, create a fresh environment with a supported Python release, reinstall your dependencies there, and point your editor, notebook, deployment command, or process manager to that environment. This is also a good moment to compare the command output with our guide on how to check your Python version.

Python Pool infographic aligning Python, pip, langchain, provider packages, and the active interpreter
Install target: Python Pool infographic aligning Python, pip, langchain, provider packages, and the active interpreter.

Install provider packages separately

LangChain now keeps many integrations in separate packages. Installing only langchain is enough for the core package, but it may not install the provider module used by your code. For example, imports from langchain_openai require the langchain-openai distribution. Use the same active interpreter pattern here too.

import subprocess
import sys

subprocess.check_call([
    sys.executable,
    "-m",
    "pip",
    "install",
    "-U",
    "langchain-openai",
])

Choose the provider package that matches your imports. A missing provider package can look similar to a missing LangChain install, especially after copying examples from a newer tutorial into an older project. The LangChain v1 release notes and migration guide are useful references when you are updating older imports.

Python Pool infographic showing LangChain core, community integrations, provider SDKs, and compatible versions
Version set: LangChain core, community integrations, provider SDKs, and compatible versions.

Verify installed packages and local shadowing

Once installation succeeds, inspect package metadata and check for local files that can shadow the real package. A project file named langchain.py, a folder named langchain, or a stale notebook working directory can cause confusing import behavior. Rename local files that conflict with package names, then clear generated __pycache__ files if necessary.

from importlib import metadata
from pathlib import Path

for package in ["langchain", "langchain-core", "langchain-openai"]:
    try:
        print(f"{package}: {metadata.version(package)}")
    except metadata.PackageNotFoundError:
        print(f"{package}: not installed in this environment")

for name in ["langchain.py", "langchain"]:
    candidate = Path.cwd() / name
    if candidate.exists():
        print(f"Possible local shadowing path: {candidate.resolve()}")

If a shadowing path appears, rename it to something project-specific, such as chains.py or llm_workflow.py. For broader import troubleshooting, see our guides on Python imports from another directory and conditional imports in Python.

Use current import paths

If LangChain is installed but an import still fails, check whether the code uses an outdated path. Modern examples often import prompts and core interfaces from langchain_core, while provider clients come from separate integration packages. This structure keeps the base package smaller and makes dependency ownership clearer.

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_template(
    "Explain {topic} in one sentence"
)
model = ChatOpenAI(model="gpt-4o-mini")

chain = prompt | model
print(chain)

You do not need an API call just to prove that these imports resolve. Building the chain is enough to confirm that Python can import the installed packages and that your environment is using current package names.

Python Pool infographic checking interpreter path, package metadata, dependency lock, and a minimal import
Import checks: Python Pool infographic checking interpreter path, package metadata, dependency lock, and a minimal import.

Deployment checklist

  • Confirm the application command uses the same Python environment where LangChain is installed.
  • Restart the notebook kernel, app server, worker, or scheduled job after installing packages.
  • Pin compatible dependency versions in requirements.txt, pyproject.toml, or your deployment lock file.
  • Install only the provider packages your project needs instead of relying on old bundled dependency assumptions.
  • Remove local files or folders that reuse package names.

Summary

To fix No module named langchain, install langchain with the exact interpreter that runs your project, verify that Python is at least 3.10, add any needed provider package such as langchain-openai, and update old imports to the current package layout. If the error survives those steps, inspect the runtime path and look for local shadowing files before changing application code.

Prove Which Python Environment Runs the Code

Installing with a bare pip command can target a different interpreter from the one used by a notebook, IDE, cron job, or web worker. Check the runtime first, then install through that executable.

import sys

print(sys.executable)
print(sys.version)

# In the same environment, run: python -m pip show langchain

If the package is installed but the import still fails, compare langchain.__file__, the active environment, and your import statement. Provider integrations are separate packages in current LangChain documentation, so install only the integration your application actually uses.

Frequently Asked Questions

Why does pip install langchain but import still fail?

The pip command may have installed LangChain into a different Python environment. Use the exact interpreter that runs the program, such as python -m pip or sys.executable -m pip.

What is the current basic LangChain install command?

For a current Python environment, install the base package with python -m pip install -U langchain, then add the provider integration package required by the application.

How do I fix the error in Jupyter?

Install into the notebook kernel’s interpreter, restart the kernel, and verify sys.executable and langchain.__file__ from a notebook cell.

Why did an old LangChain import stop working?

LangChain’s package layout and integrations have changed. Check current documentation for the specific module and provider instead of assuming every older tutorial import is still valid.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted