Fix No Module Named ‘langchain.chains’
Fix ModuleNotFoundError: No module named 'langchain.chains' by matching your imports to your installed LangChain package: with the newer package layout, legacy chains belong in langchain-classic and are imported from langchain_classic.chains. If your code already matches its dependencies, check whether it is running in the wrong Python environment or importing a local file instead of LangChain. Use the exact error message to choose the right repair before reinstalling anything.
| Symptom | Likely Cause | Fix |
|---|---|---|
No module named 'langchain.chains', but importing langchain works |
Older code is running against the newer package layout | Install langchain-classic and update the chain imports |
No module named 'langchain' |
The package is absent from the running environment | Install the project’s dependencies into that interpreter |
| The import works in a terminal but fails in a notebook or IDE | Different interpreters or notebook kernels | Select the intended environment or install into the active kernel |
The error includes 'langchain' is not a package |
A local file is shadowing the installed package | Rename the conflicting file and restart Python |
cannot import name 'Memory' from 'langchain.chains.base' |
Python found the module, but the requested name is unavailable | Check the symbol’s supported import path, not just package installation |
Why Python Cannot Find langchain.chains
A missing module and a missing name are different failures; check the full exception and the running interpreter before reinstalling anything.
Python treats the following import as a request to find a package named langchain, locate its chains submodule, and retrieve LLMChain:
from langchain.chains import LLMChain
Installing a distribution named langchain does not guarantee that every historical submodule still exists. LangChain’s newer package structure separates core abstractions, integrations, and legacy functionality. Older examples can therefore fail even when the installed package is healthy.
There is also an important distinction between installation names and import names:
langchain-classicis the distribution you install.langchain_classicis the Python package you import.langchain.chainsis an older import path, not a separate distribution to install.
Installing langchain-community does not restore langchain.chains. Community integrations and legacy chains serve different purposes.
A Missing Module Is Not a Missing Class
Read the final exception line alongside the import that triggered it. A traceback can begin with a chain import but end with a different missing dependency; that final missing module is the immediate failure.
For example, with LangChain 0.0.106 and Python 3.9, the historical Memory import failure involved this statement:
from langchain.chains.base import Memory
It raised ImportError: cannot import name 'Memory' from 'langchain.chains.base'. Python had already found langchain.chains.base; the requested name was the problem. That historical error is not evidence that the entire chains module needs installing, and its old replacement import should not be treated as current guidance.
The Installed Package May Not Be the Imported Package
A successful installation only affects the environment targeted by that installer. Your IDE, notebook, application server, or container may run a different interpreter.
Python also searches locations associated with your script. A project file named langchain.py can take precedence over the installed package. In that situation, reinstalling LangChain leaves the conflicting filename untouched.
How to Fix the langchain.chains Import Error
Use the application’s Python interpreter, resolve local naming conflicts, then install langchain-classic and change legacy imports to langchain_classic.chains.
Step 1: Check the Interpreter and Installed Packages
- Run this using the Python interpreter that starts the failing application:
python -c "import sys; print(sys.executable)"If your application uses a differently named executable or an explicit interpreter path, use that executable instead of
pythonthroughout these terminal commands. - Inspect the relevant installed distributions:
python -m pip show langchain langchain-classic langchain-coreCheck the versions and installation locations. A warning that
langchain-classicis not installed is useful information, not a reason to reinstall everything. Usingpython -m pipbinds pip to the selected Python interpreter. A standalone pip command can target another environment. - Find the package Python actually imports:
python -c "import langchain; print(langchain.__file__)"For a normal installed package, the path should lead into the intended environment’s installed packages. A path pointing to your project’s
langchain.pyor an unintended locallangchaindirectory indicates a naming conflict. An editable development installation can legitimately point into a source checkout, so judge the path against how the project was installed.
Step 2: Correct Environment and Filename Conflicts
- If the interpreter is wrong, select the project’s intended environment in your IDE or use that environment’s Python executable in the terminal. Changing an IDE terminal does not necessarily change its debugger or notebook interpreter.
- If a local module is shadowing LangChain, rename the conflicting file or directory to something specific to your application. Update your own imports if they reference the renamed module. Do not rename or delete the real package inside the environment’s installed packages.
- Restart the Python process after changing an interpreter or resolving a filename conflict. For a notebook, restart the kernel.
- Run the failing import again. If it now works, run the application normally; no package migration is needed. If the correct environment still cannot find
langchain.chains, address the package layout next.
Step 3: Install the Compatibility Package and Update Your Imports
For code you maintain that still uses legacy chain classes, the compatibility-package approach preserves those APIs without pretending the old module path still exists.
- Install the compatibility package into the application’s environment:
python -m pip install langchain-classicMake this change in the project’s isolated environment. If the project has a lockfile or strict dependency constraints, add the dependency through its normal dependency-management workflow rather than bypassing those constraints.
- Replace legacy chain imports. For example, change:
from langchain.chains import LLMChain, RetrievalQATo:
from langchain_classic.chains import LLMChain, RetrievalQALegacy retrieval helpers also use the compatibility namespace:
from langchain_classic.chains import create_retrieval_chain from langchain_classic.chains.combine_documents import create_stuff_documents_chainUse only the classes and helpers your application actually needs.
- Search your project for additional imports beginning with
langchain.chains. An application may import the old path in a utility module even after you fix its main script. - Record
langchain-classicas a direct project dependency if your code imports it directly.
Installing the compatibility package does not recreate the old import path. Leaving an import pointed at langchain.chains can produce the same error after installation.
Avoid replacing every occurrence of langchain with langchain_classic. Prompt templates, messages, integrations, and other APIs have their own supported namespaces. The compatibility package is useful for retaining legacy functionality, not a universal replacement for the entire ecosystem.
Step 4: Test the Imports and Restart the Application
- Verify the corrected chain imports without making a model request:
python -c "from langchain_classic.chains import LLMChain, RetrievalQA; print('Chain imports OK')"Successful output confirms that these classes are importable in that interpreter. It does not require an API key.
- Check installed dependency requirements:
python -m pip checkThe expected successful result is
No broken requirements found.If pip identifies conflicts, resolve them through the project’s declared dependency set before treating the environment as repaired. - Restart any notebook kernel, development server, worker, or other long-running process that loaded the previous packages.
- Launch the application through its usual entry point. A successful standalone import is necessary, but the application may contain additional outdated imports that only execute during startup or a particular feature.
An API-key error or provider configuration error at this point is a separate issue. Do not undo a working import repair merely because execution has progressed to a later failure.
When Notebooks, IDEs, or Dependencies Still Fail
Match the active kernel or run configuration to the intended interpreter, then update or restore dependencies that contain old imports.
Jupyter and Colab Use a Different Kernel
A notebook’s Python interpreter may differ from the interpreter in your terminal.
- Inspect the active kernel:
import sys print(sys.executable) - In an IPython-based notebook, install into the current kernel’s environment:
%pip install langchain-classic - Change the notebook’s legacy chain imports to the compatibility namespace, then restart the kernel.
- Verify the import in a fresh cell:
from langchain_classic.chains import LLMChain print("Chain import OK") - Run the notebook from the beginning so its objects are recreated under the repaired environment.
If installation produces dependency conflicts, resolve those conflicts before rerunning the full notebook. Repeatedly installing into an unrelated terminal environment will not change the active kernel.
VS Code or PyCharm Runs Another Interpreter
Check the interpreter selected for the actual run configuration, not just the integrated terminal. Debug sessions, notebook sessions, and terminal commands can use different environments within the same editor.
Select the project’s intended interpreter, install dependencies through that interpreter, and update the source imports if the project uses the newer package layout. Then stop the existing debug or run session and launch the application again.
If the error persists, temporarily add `import sys` followed by `print(sys.executable)` inside the failing application to identify its interpreter. The executable used by the failing process is more useful than the one an unrelated terminal happens to select.
A Third-Party Dependency Contains the Old Import
If the traceback points to an old import inside an installed dependency, changing your application’s imports will not repair that dependency.
Choose between these approaches:
- Update the dependency to a release that supports your intended LangChain package layout. Update it through your project’s dependency manager, resolve any resulting conflicts, restart the application, and test the failing feature.
- Restore the project’s known-compatible dependencies if the application needs its original stack. Use its supplied lockfile or pinned dependency file rather than guessing a LangChain version.
For a project whose supplied, pinned dependencies are in requirements.txt, install them in its intended environment:
python -m pip install -r requirements.txt
Then check consistency:
python -m pip check
Restart the application and exercise the feature that previously failed. A requirements file with unpinned dependencies does not guarantee restoration of an older working environment.
Avoid editing installed library files as a permanent solution. Those edits disappear when the dependency is reinstalled and are not reproduced for teammates or deployments.
An Older Tutorial Assumes an Older Package Layout
A tutorial’s code and its dependency versions form a set. Mixing old imports with current packages can produce several errors in succession: chains first, then prompts, memory, or schema.
For maintained code, update the relevant imports and declare the packages you now use. For an exact reproduction, use the tutorial’s supplied dependency set in a separate environment, then run its example to completion.
Do not choose an arbitrary old LangChain version just because it contains langchain.chains. Other dependencies may require a different package generation.
How to Confirm the Fix and Keep It Working
The repair is complete when all of these checks pass:
- The application runs under the intended interpreter.
- Imported modules resolve to the intended installation rather than conflicting local files.
- The actual chain imports used by the application succeed.
- Pip reports no broken declared requirements.
- The original startup command and previously failing feature both work.
Package metadata alone is not enough. A distribution can appear installed while your application still uses the wrong import path, and pip’s dependency check does not test whether your source code uses supported APIs.
Keep direct dependencies in the project’s dependency declaration and record the resolved environment through its normal locking workflow. Test that declared environment when rebuilding a container or deploying the application; packages installed manually on a development laptop are not automatically present elsewhere.
After the import is repaired, run the smallest application-level test that uses the affected chain, then save the code and dependency changes together.
Frequently Asked Questions
Why Does Python Say langchain.prompts Is Missing?
In the split-package layout, prompt templates are available from langchain_core.prompts; an older langchain.prompts import may no longer match the installed package. Replace the import with the supported namespace:
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
If langchain_core itself is missing, install its distribution into the application’s interpreter:
python -m pip install langchain-core
Restart the process and rerun the corrected import.
How Do I Fix No Module Named 'langchain_community'?
Install the separate langchain-community distribution into the environment running your application:
python -m pip install langchain-community
Restart the process and verify that the package imports:
import langchain_community
print("Community package import OK")
Individual integrations can require additional dependencies, so a later error naming another package needs its own diagnosis. Installing community integrations does not restore the old langchain.chains namespace.
How Do I Fix No Module Named 'langchain.memory'?
For a missing langchain.memory module, check the memory class’s supported import path for the installed package. If the application also uses legacy chains, install their compatibility package:
python -m pip install langchain-classic
For example, a legacy conversation-buffer import becomes:
from langchain_classic.memory import ConversationBufferMemory
print("Memory import OK")
Restart the process and rerun the affected code. Using the compatibility namespace is a compatibility repair; it does not make legacy memory classes the preferred design for a new application.
What Replaces Imports From langchain.schema?
Replace a langchain.schema import according to the object you need, rather than swapping the entire namespace blindly. Common document and message types belong to these core modules:
from langchain_core.documents import Document
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
If the core package is absent, install it into the application’s interpreter:
python -m pip install langchain-core
Restart the process, test the corrected imports, and verify each additional schema object against the API for your installed package.
