3

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

Brandon
  • 308
  • 1
  • 2
  • 10

5 Answers5

8

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.

dirbacke
  • 2,861
  • 21
  • 25
  • Thanks for the assist! Turns out I simply needed to update my ipynb kernel: https://stackoverflow.com/a/75545398/1130817 – Brandon Apr 01 '23 at 17:01
5

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 https://stackoverflow.com/a/75545398/1130817

Brandon
  • 308
  • 1
  • 2
  • 10
0

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

Dattatray
  • 120
  • 1
  • 5
0

Try this:

from langchain.document_loaders import DirectoryLoader
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    Your answer could be improved by adding more information on how the proposed solution solves the problem and how it helps the OP. – Tyler2P Jun 17 '23 at 18:15
0

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