python包chromadb安装失败总结

1,背景:

最近在学习langchain的课程,里面创建自己的知识库的Retrieval模块中,需要用到向量数据库。
所以按照官方的教程(vectorstores),准备使用chroma的向量数据库。
图片来源

python包chromadb安装失败总结_第1张图片

2,问题1:pip安装出错

现象

第一步安装python包的时候,出现了下面的错误:

pip install chromadb

Looking in indexes: https://mirror.baidu.com/pypi/simple/, https://mirrors.aliyun.com/pypi/simple/, https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting chromadb
  Downloading https://mirrors.aliyun.com/pypi/packages/7c/cc/8b822be150323492e1d3c2ae46ccd99ddc9841894afdc41c408ffd68918e/chromadb-0.4.22-py3-none-any.whl (509 kB)
......
Collecting importlib-metadata<7.0,>=6.0 (from opentelemetry-api>=1.2.0->chromadb)
  Downloading https://mirrors.aliyun.com/pypi/packages/59/9b/ecce94952ab5ea74c31dcf9ccf78ccd484eebebef06019bf8cb579ab4519/importlib_metadata-6.11.0-py3-none-any.whl (23 kB)

Installing collected packages: pypika, mpmath, monotonic, mmh3, flatbuffers, wrapt, websocket-client, uvloop, urllib3, tomli, sympy, python-dotenv, pyasn1, pulsar-client, overrides, opentelemetry-util-http, opentelemetry-semantic-conventions, opentelemetry-proto, oauthlib, importlib-metadata, humanfriendly, httptools, grpcio, googleapis-common-protos, chroma-hnswlib, bcrypt, backoff, asgiref, watchfiles, rsa, pyproject_hooks, pyasn1-modules, opentelemetry-exporter-otlp-proto-common, deprecated, coloredlogs, requests-oauthlib, posthog, opentelemetry-api, onnxruntime, google-auth, build, tokenizers, opentelemetry-sdk, opentelemetry-instrumentation, kubernetes, opentelemetry-instrumentation-asgi, opentelemetry-exporter-otlp-proto-grpc, opentelemetry-instrumentation-fastapi, chromadb
  Attempting uninstall: urllib3
    Found existing installation: urllib3 2.0.7
    Uninstalling urllib3-2.0.7:
      Successfully uninstalled urllib3-2.0.7
  Attempting uninstall: importlib-metadata
    Found existing installation: importlib-metadata 7.0.1
    Uninstalling importlib-metadata-7.0.1:
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: 'RECORD'
Consider using the `--user` option or check the permissions.
原因分析

看上面的红色的部分,安装的时候,要求importlib-metadata的版本:6.0<=版本<7,但是环境中默认的是7.0.1,所以安装的时候想要把7的版本卸载,安装6的版本,问题出在卸载这里,没有权限卸载。
为什么没有权限卸载?我用的是百度平台的BML codeLab,创建ipynb文件的时候选择的内核如下,所以环境自带了python3的环境。(这个包是python的一个标准库importlib-metadata),猜测可能是作为python环境的一部分, codeLab这边不允许修改。

python包chromadb安装失败总结_第2张图片

解决方案

按照出错中的提示(上图出错的黄色的部分):
使用user模式进行python包的安装,正常安装成功。

!pip install --user chromadb

补充:
我有点好奇,使用这个--user和没有使用,有啥区别?
后来发现,两者的位置不同。

  • 没有--user的时候,安装到默认的python包的路径下面。
    可以用pip show pkname(找一个有的包)查看,python包的安装路径
    codelab中默认的路径是:/opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages
  • 有--user的时候,安装到用户的目录下面,
    通过pip show pkname的命令查看--user模式安装好的包的路径。
    codelab中对应的--user模式下的路径是:
    /home/aistudio/.local/lib/python3.10/site-packages

3,问题2:导入模块失败(ModuleNotFoundError : No module named 'chromadb')

现象

上面安装之后,正准备开开心心继续的时候,发现虽然安装成功(使用pip show能够正常看到包的信息),但是使用Langchain生成数据库的实例的时候,会报错:
原因是在langchain中引入chromadb的模块时候,提示模块不存在。(import chromadb失败)

# 定义一个向量数据库的实例

vectorDB = Chroma(

        collection_name="langchain_store",

        embedding_function=qianfan_embedding_model)

python包chromadb安装失败总结_第3张图片

原因分析

python导包的时候,会查找内置模块,以及sys.path中的路径。
确认一下sys.path的设定,里面是没有上面安装的本地用户的路径的,
所以虽然包正常安装,但是import的时候,找不到,所以报错。
 

import sys

print(sys.path)

['/home/aistudio', '/opt/conda/envs/python35-paddle120-env/lib/python310.zip', '/opt/conda/envs/python35-paddle120-env/lib/python3.10', '/opt/conda/envs/python35-paddle120-env/lib/python3.10/lib-dynload', '', '/opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages']
解决方案

把--user模式下的安装路径加入到sys.path中。
不同的环境下,安装的路径可能有所不同,使用命令[pip show 包名] 查看安装的路径。

sys.path.append('/home/aistudio/.local/lib/python3.10/site-packages')

之后就可以正常import了。

4,后记

这个问题搞了一上午,整体来看,对于pip安装的细节(比如安装路径),以及安装之后,import导入的细节(包是怎么查找的)不是特别清楚,导致调查花了一些时间。
mark一下,也希望分享给有类似问题的小伙伴。

你可能感兴趣的:(python,开发语言)