langchain: No module named 'langchain.document_loaders'

First and foremost I'm using the latest of Python (==3.11.2) and the most recent version of langchain (==0.0.128).

Following the latest docs on DirectoryLoader, the following line should work:

from langchain.document_loaders import DirectoryLoader

Instead, I'm seeing the following error. Any suggestions?

enter image description here

6 Answers

The ModuleNotFoundError typically occurs when Python cannot find the module you are trying to import.

Assuming that you have already installed langchain using pip or another package manager, the issue might be related to the way you are importing the module. Here are a few things you can try:

  1. Make sure that langchain is installed and up-to-date by running
pip install --upgrade langchain 
  1. Check that the installation path of langchain is in your Python path. You can check this by running the following code:
import sys print(sys.path) 

The output should include the path to the directory where langchain is installed. If it does not, you can add the path using sys.path.append('<path_to_langchain_installation>').

  1. Double-check that you are importing DirectoryLoader from the correct package. In the latest version of langchain, DirectoryLoader is located in the langchain.loaders module, so you should use the following import statement:
from langchain.loaders import DirectoryLoader 

If you are still having trouble, you can try uninstalling and reinstalling langchain to make sure that the installation is not corrupted.

1

Turns out that the ipynb kernel was using Python 3.7 instead of Python 3.11, even though the 3.11 was the default install.

I was able to verify this by running

from platform import python_version print(python_version()) 

And fixed via

if you want to load onlt .txt files from directory you can use

loader = DirectoryLoader('./training', glob='**/*.txt') 

where './training' will be path of directory containing .txt file

Try this:

from langchain.document_loaders import DirectoryLoader 
1

For me the cause was, I didn't add python path to my system environment.

1

I am using Python 3.11.5 and I run into this issue with ModuleNotFoundError: No module named 'langchain.document_loaders' after running pip install 'langchain[all]', which appears to be installing langchain-0.0.39

If I then run pip uninstall langchain, followed by pip install langchain, it proceeds to install langchain-0.0.308 and suddenly my document loaders work again.

  • Why does langchain 0.0.39 have document_loaders broken?
  • Why does langchain[all] install a different version of langchain?

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like