Python cannot import LangChain right after pip installed it
Fix it fast
Most common cause, 1 of 3pip put LangChain in another Python installation, not the one your code runs on
Call pip through the exact interpreter path that sys.executable printed.
/path/printed/by/sys.executable -m pip install -U langchain
# in a Jupyter cell
%pip install -U langchainShort 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.
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.
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:
- 1pip put LangChain in another Python installation, not the one your code runs on
sys.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 - 2The code imports a provider package that pip install langchain does not include
import langchain works, but the error names langchain_openai, langchain_anthropic, langchain_community or another langchain_ module - 3A file or folder named langchain in the project hides the real package
The 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.
/path/printed/by/sys.executable -m pip show langchainFix
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.
/path/printed/by/sys.executable -m pip install -U langchain
# in a Jupyter cell
%pip install -U langchainVerify
The same interpreter now finds the package and reports its location under its own site-packages.
/path/printed/by/sys.executable -m pip show langchainConfirmed by pip.pypa.io
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.
python -m pip show langchain | grep -i ^requiresFix
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.
python -m pip install -U langchain-openai # import langchain_openai
python -m pip install -U langchain-anthropic # import langchain_anthropicVerify
The provider module imports in the same interpreter.
python -c "import langchain_openai; print('ok')"Confirmed by docs.langchain.com
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.
ls langchain.py langchain/ __pycache__/langchain*.pyc 2>/dev/nullFix
Give your file or folder a name no installed package uses, remove its cached bytecode, and rerun the script.
mv langchain.py langchain_demo.py
rm -f __pycache__/langchain*.pycVerify
find_spec now points to site-packages, and the import from langchain.agents succeeds.
python -c "import importlib.util; print(importlib.util.find_spec('langchain').origin)"Confirmed by docs.python.org
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.