Langchain:ModuleNotFoundError:没有名为“langchain”的模块

ano*_*sqs 5 python import module python-import langchain

当我在 VS Code 中编写代码时,从以下内容开始:

import os
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import TextLoader
Run Code Online (Sandbox Code Playgroud)

我遇到了错误:ModuleNotFoundError: No module named 'langchain'

我已将 Python 更新到版本 3.11.4,更新了 pip,并重新安装了 langchain。C:\\Python311\\Lib\\site-packages我还检查了 sys.path 并附加了 Langchain 文件夹所在的文件夹。

编辑:当我在 Python 控制台中运行 Langchain 导入时(功能也有效),但当我从 VSCode 运行按钮运行代码时,它仍然提供 ModuleNotFoundError。

还有其他人遇到过这个问题并找到解决方案吗?

Yil*_*maz 6

我已经安装了软件包,python 3.9.7但这个版本引起了问题,所以我切换到了Python 3.10。当我安装它时,langhcain它位于python 3.9.7目录中。如果你跑步pip show langchain,你会得到这个

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
Run Code Online (Sandbox Code Playgroud)

如果您查看该Location属性,您会看到这一点/home/anaconda3/lib/python3.9/site-packages。但由于我正在使用,Pyhton3.10我必须确保langchain位于Python 3.10. 所以安装langhchain

python3.10 -m pip install langchain   
Run Code Online (Sandbox Code Playgroud)

现在当我跑步时,python3.10 -m pip show langchain我得到这个

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: 
Run Code Online (Sandbox Code Playgroud)

现在 newLocation指的是Python3.10目录


ano*_*sqs 0

为了将来参考,我相当确定此错误是由于 pip 将文件安装在错误的目录中引起的。确保目标目录是 sys.path 文件夹所在的位置,因为有时这些目录可能会有所不同!感谢所有提供建议的人。