Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

Python cannot import LangChain right after pip installed it

Your error

ModuleNotFoundError: No module named 'langchain'2 more lines it can show

Fix it fast

Most common cause, 1 of 3

pip put LangChain in another Python installation, not the one your code runs on

Call pip through the exact interpreter path that sys.executable printed.

Fix
/path/printed/by/sys.executable -m pip install -U langchain

# in a Jupyter cell
%pip install -U langchain
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ModuleNotFoundError: No module named 'langchain' (or 'langchain_openai') after a successful pip install means pip wrote into another Python installation, not the one executing your script, the import names a provider package langchain does not pull in, or a local langchain.py shadows the package. Install with python -m pip from that interpreter, add the provider package, or rename the file. Applies to langchain 1.5.

  • Applies to langchain 1.5.2 (requires Python 3.11+), pip 26.3 run as python -m pip, Jupyter kernels with IPython %pip
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run these two lines in the place that fails (the IDE's run button, the notebook cell or the server process), not in a fresh terminal. A different interpreter path from the one you installed with points to the first cause; langchain found while the error names a langchain_ module points to the second; a file path inside your project points to the third.

Run this first
import sys; print(sys.executable)
import importlib.util; print(importlib.util.find_spec("langchain"))

Then find the line you see in this list and open its fix:

  1. 1pip put LangChain in another Python installation, not the one your code runs onsys.executable in the failing process is not the interpreter that pip used, for example a system Python versus the project's .venv, or a notebook kernel versus the terminal
  2. 2The code imports a provider package that pip install langchain does not includeimport langchain works, but the error names langchain_openai, langchain_anthropic, langchain_community or another langchain_ module
  3. 3A file or folder named langchain in the project hides the real packageThe message ends with 'langchain' is not a package, or find_spec shows a path inside your project instead of site-packages

What does the error look like?

  • ModuleNotFoundError: No module named 'langchain'
  • ModuleNotFoundError: No module named 'langchain_openai'
  • ModuleNotFoundError: No module named 'langchain.agents'; 'langchain' is not a package

Cause 1 of 3

pip put LangChain in another Python installation, not the one your code runs on

You seesys.executable in the failing process is not the interpreter that pip used, for example a system Python versus the project's .venv, or a notebook kernel versus the terminal

Check

No module named 'langchain' after a clean install usually means two interpreters. python -m pip runs pip for exactly the interpreter you name, while a bare pip command runs for whichever Python installed that command. IDEs and Jupyter kernels often run yet another Python, so the package lands in one site-packages and the import searches another.

Check
/path/printed/by/sys.executable -m pip show langchain

Fix

Call pip through the exact interpreter path that sys.executable printed. Inside a notebook, the %pip magic runs pip inside the current kernel. If that interpreter is Python 3.10 or older, pip quietly picks an older LangChain, since langchain 1.5 requires Python 3.11, so move the project to a newer Python first.

Fix
/path/printed/by/sys.executable -m pip install -U langchain

# in a Jupyter cell
%pip install -U langchain

Verify

The same interpreter now finds the package and reports its location under its own site-packages.

Verify
/path/printed/by/sys.executable -m pip show langchain

Confirmed by pip.pypa.io

Did this fix it?

Cause 2 of 3

The code imports a provider package that pip install langchain does not include

You seeimport langchain works, but the error names langchain_openai, langchain_anthropic, langchain_community or another langchain_ module

Check

LangChain keeps its model and tool integrations in independent provider packages, and langchain 1.5.2 itself depends only on langchain-core, langgraph and pydantic. Code copied from the docs that starts with from langchain_openai import ChatOpenAI therefore needs a second install, under the package name with a hyphen.

Check
python -m pip show langchain | grep -i ^requires

Fix

Install each provider package the imports name, using hyphens in the pip name and underscores in the import. Prefer a dedicated provider package over langchain-community, which its maintainers are sunsetting.

Fix
python -m pip install -U langchain-openai      # import langchain_openai
python -m pip install -U langchain-anthropic   # import langchain_anthropic

Verify

The provider module imports in the same interpreter.

Verify
python -c "import langchain_openai; print('ok')"

Confirmed by docs.langchain.com

Did this fix it?

Cause 3 of 3

A file or folder named langchain in the project hides the real package

You seeThe message ends with 'langchain' is not a package, or find_spec shows a path inside your project instead of site-packages

Check

Python puts the directory of the script being run at the front of sys.path, ahead of site-packages, so a langchain.py or langchain folder next to the script is imported instead of the installed package. A plain file cannot hold submodules, which is why Python adds 'langchain' is not a package when you import langchain.agents from it.

Check
ls langchain.py langchain/ __pycache__/langchain*.pyc 2>/dev/null

Fix

Give your file or folder a name no installed package uses, remove its cached bytecode, and rerun the script.

Fix
mv langchain.py langchain_demo.py
rm -f __pycache__/langchain*.pyc

Verify

find_spec now points to site-packages, and the import from langchain.agents succeeds.

Verify
python -c "import importlib.util; print(importlib.util.find_spec('langchain').origin)"

Confirmed by docs.python.org

Did this fix it?

How do I stop it coming back?

  • Create one virtual environment per project and always install with python -m pip (or your project tool) from inside it.
  • Write each provider package your imports rely on into the project's dependency list, beside langchain.
  • Set requires-python to 3.11 or newer in pyproject.toml so an old interpreter fails loudly instead of resolving an older LangChain.
  • Never name project files after libraries you import, such as langchain.py or openai.py.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.