4

When I write code in VS Code, beginning with:

import os
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import TextLoader

I am met with the error: ModuleNotFoundError: No module named 'langchain'

I have updated my Python to version 3.11.4, have updated pip, and reinstalled langchain. I have also checked sys.path and the folder C:\\Python311\\Lib\\site-packages in which the Langchain folder is, is appended.

EDIT: Langchain import works when I run it in the Python console (functionality works too), but when I run the code from the VSCode run button it still provides the ModuleNotFoundError.

Has anyone else run into this issue and found a solution?

5 Answers5

1

For future reference, I'm fairly sure this error was caused by pip installing files in the incorrect directory. Make sure the target directory is where your sys.path folder is, as sometimes these can vary! Thanks to everyone who provided advice.

1

I had installed packages with python 3.9.7 but this version was causing issues so I switched to Python 3.10. When I installed the langhcain it was in python 3.9.7 directory. If yo run pip show langchain, you get this

Name: langchain
Version: 0.0.220
Summary: Building applications with LLMs through composability
Home-page: https://www.github.com/hwchase17/langchain
Author: 
Author-email: 
License: MIT
Location: /home/anaconda3/lib/python3.9/site-packages
Requires: aiohttp, async-timeout, dataclasses-json, langchainplus-sdk, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity
Required-by: jupyter_ai, jupyter_ai_magics

If you look at the Location property, you see this /home/anaconda3/lib/python3.9/site-packages. But since I am using Pyhton3.10 I had to make sure langchain is in the directory of Python 3.10. so installed the langhchain with

python3.10 -m pip install langchain   

now when I run, python3.10 -m pip show langchain I get this

Name: langchain
Version: 0.0.264
Summary: Building applications with LLMs through composability
Home-page: https://www.github.com/hwchase17/langchain
Author: 
Author-email: 
License: MIT
Location: /home/.local/lib/python3.10/site-packages
Requires: aiohttp, async-timeout, dataclasses-json, langsmith, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity
Required-by: 

Now new Location is referring to Python3.10 directory

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
0

you probably didn't put your python file location into the enviroment variables.

copy your python file location go to search>edit enviroment enviroment variables>user variables>path>new put the copied python location in the user variables. then try again!

DarkStar
  • 1
  • 2
0

when installing in your environment try using: python -m pip install langchain

Jorge M
  • 11
  • 2
0

If you are running the code in VS and you have more than one Python version, please check if the bottom right side of the VS Window the version of Python you are using.

waloar
  • 108
  • 1
  • 1
  • 9