What is modulenotfounderror: no module named ‘langchain’?
The ModuleNotFoundError: No module named ‘langchain’ error message typically occurs when you are trying to import the “langchain” module.
However, the module is not installed in your Python environment or it is installed in a different environment.
So, that is why the error arises because Python cannot find the “langchain” module.

Why does this error occur?
This error could occur due to several reasons:
- Langchain is not installed
You can check if “langchain” is installed
- Python version mismatch
If you have multiple versions of Python installed, the “langchain” module might be installed under a different version than the one you’re using.
- Incorrect import statement
Ensure you are importing the correct module.
- Python path issues
Python might not be looking in the correct directory for the “langchain” module. You can check your Python path by running import sys; print(sys.path) in Python.
How to fix modulenotfounderror: no module named ‘langchain’?
To fix the no module named ‘langchain’, here are some steps you can take to resolve this error:
Solution 1: Check if Langchain is installed
You can check if the “langchain” module is installed by running the following command in your terminal.
pip show langchain or
pip show langchain Solution 2: Install the module
If it’s not installed, you can install it using the following command:
pip install langchainor
pip3 install langchainUse the following command, if you are using for Jupyter Notebook:
!pip install langchain
If the module is up-to-date, you can upgrade it using the following command:
pip install --upgrade langchainSolution 3: Check your Python version
If you have multiple versions of Python installed, the “langchain” module might be installed under a different version than the one you’re using.
You can check your Python version using the following command:
python --versionpython 3 --versionAnd ensure that “langchain” module is installed under the correct version.
Solution 4: Check your import statement
Ensure you are importing the correct module.
For example, in the latest version of “langchain,” DirectoryLoader is located in the langchain.loaders module, so you should use:
from langchain.loaders import DirectoryLoader
Solution 5: Check your Python path
Python might not look in the correct directory for the “langchain” module.
You can check your Python path by running the following command:
import sys; print(sys.path)in Python.
If the path to the directory where “langchain” is installed is not included, you can add it using the following:
sys.path.append('<path_to_langchain_installation>')Solution 6: Reinstall Langchain
If none of these solutions work in your case, you can try uninstalling and reinstalling “langchain” the module to ensure the installation is not corrupted.
To uninstall:
pip uninstall langchainor
pip3 uninstall langchainTo reinstall:
pip install langchainor
pip3 install langchainDon’t forget to substitute with the real directory where “langchain” is located.
Also, if you’re utilizing a virtual environment, it’s crucial to activate it before the installation of “langchain”.
Conclusion
The ModuleNotFoundError: No module named ‘langchain’ error message typically occurs when trying to import the “langchain” module.
By following the solutions provided above, you should be able to resolve this error quickly.
Please note that the steps given are general, and the exact solution might vary depending on the specifics of your project and environment.
If you encounter another ModuleNotFoundError, it’s important to analyze it before troubleshooting.
We hope that this guide has helped you resolve the error. Thank you for reading, and enjoy your coding journey!
Diagnostic checklist for “No module named ‘langchain'”
- Verify pip install target. Run
pip show langchain— if not installed, runpip install langchain. - Check the active Python interpreter.
which python(mac/Linux) orwhere python(Windows). Both pip and python must point to the same environment. - Check virtual environment activation. If you use venv/conda, activate before installing:
source .venv/bin/activate. - Rule out uppercase/lowercase. Python imports are case-sensitive:
import PyPDF2notimport pypdf2. - Rule out the pip-vs-package-name mismatch. Some packages install under a different name than you import (e.g.
pip install beautifulsoup4→import bs4).
Modern install for LLM frameworks
# 2026 recommended workflow — uv is fastest pip install uv uv pip install langchain # Or classic pip pip install langchain # With extras for LLM providers pip install "langchain[openai,anthropic]"
Common “No module named ‘langchain'” causes
- Version pinning conflict. LLM libraries update fast —
pip install --upgrade langchainif you saw the module before. - Multiple Python versions. LLM tutorials often use Python 3.11 or 3.12. Verify
python --versionmatches. - Notebook kernel. Jupyter picks a different kernel than pip installs to. Use
%pip install langchaininside the notebook. - WSL / Docker paths. Install in the same environment as your Python — not on the host if you run Python inside WSL.
Working code example
# Verify install import langchain print(langchain.__version__) # Basic usage skeleton # (adapt to your specific langchain version)
Best practices
- Use a virtual environment for every LLM project. Dependencies overlap and pin conflicts are common.
- Pin your versions in
requirements.txtorpyproject.toml. LLM libraries move fast. - Consider uv or Poetry. Modern package managers handle dep resolution far better than pip alone.
Official documentation
Frequently Asked Questions
What is Python ModuleNotFoundError and what causes it?
ModuleNotFoundError (a subclass of ImportError) is raised when Python cannot find the module you tried to import. Common causes: the package isn’t installed (pip install missing), wrong virtual environment activated, typo in module name, or Python can’t find your local module on the import path. The error message names exactly which module is missing.
How do I fix ‘ModuleNotFoundError: No module named X’?
Run pip install X first. If that succeeds but you still get the error, check which Python you’re using (which python OR python –version) vs which pip (which pip OR pip –version), they must match. Common gotcha: pip points to system Python 3.9 but you’re running python3.11 in a venv. Inside the venv, use python -m pip install X to be sure pip matches the active Python.
Why does my code work in one environment but not another?
Different Python versions or different installed packages. To diagnose: pip freeze > requirements.txt on the working environment, then pip install -r requirements.txt on the broken one. Use virtualenv (python -m venv venv) or conda for every project to avoid system-wide package collisions.
Is ModuleNotFoundError the same as ImportError?
ModuleNotFoundError is a subclass of ImportError added in Python 3.6. It specifically means ‘no such module exists.’ Plain ImportError covers a wider set: module exists but a name inside it can’t be imported (e.g. ‘cannot import name X from Y’). except ImportError catches both; except ModuleNotFoundError catches only the missing-module case.
Where can I find more ModuleNotFoundError fixes?
Browse the ModuleNotFoundError reference hub for 198+ specific module fixes (TensorFlow, Flask, Django, pandas, numpy, etc.). For related issues see ImportError. For broader Python setup see Python Tutorial hub.
