win10系统 python3.11.9 的 sentence_transformers安装报错解决

pip install sentence_transformers报错的解决

可以直接看 '解决方法' 这块

系统配置

我的系统是 win10专业版Python 3.11.9

问题过程

pip install sentence_transformers安装成功

但是运行代码后报错:

(.venv) PS D:\embeddings> & d:/embeddings/.venv/Scripts/python.exe d:/embeddings/gte-Qwen2-7B-instruct.py
Traceback (most recent call last):
  File "d:\embeddings\gte-Qwen2-7B-instruct.py", line 9, in <module>
    model = SentenceTransformer("Alibaba-NLP/gte-Qwen2-7B-instruct", trust_remote_code=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\SentenceTransformer.py", line 287, in __init__ 
    modules = self._load_sbert_model(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\SentenceTransformer.py", line 1487, in _load_sbert_model
    module = Transformer(model_name_or_path, cache_dir=cache_folder, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\models\Transformer.py", line 54, in __init__   
    self._load_model(model_name_or_path, config, cache_dir, **model_args)
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\models\Transformer.py", line 85, in _load_model    self.auto_model = AutoModel.from_pretrained(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 551, in from_pretrained
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ed
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 502, in get_class_from_dy  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 502, in get_class_from_dynamic_module
namic_module
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 327, in get_cached_module_file
    modules_needed = check_imports(resolved_module_file)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 182, in check_imports    
    raise ImportError(
ImportError: This modeling file requires the following packages that were not found in your environment: flash_attn. Run `pip install flash_attn`

可以看到terminal提示需要 pip install flash_attn

于是我继续pip install flash_attn

报错
ModuleNotFoundError: No module named 'torch'

(.venv) PS D:\embeddings> pip install flash_attn
Collecting flash_attn
  Using cached flash_attn-2.6.1.tar.gz (2.6 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [20 lines of output]
      Traceback (most recent call last):
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 327, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 297, in _get_build_requires
          self.run_setup()
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 497, in run_setup
          super().run_setup(setup_script=setup_script)
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 313, in run_setup
          exec(code, locals())
        File "<string>", line 19, in <module>
      ModuleNotFoundError: No module named 'torch'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.

解决方法

步骤一:

安装CUDA Toolkit以及cuda:

cuda官网的下载页面选择匹配自己系统的CUDA Toolkit进行无脑安装

步骤二:

指定 sentence-transformers版本安装
pip install sentence-transformers==2.2.0

相关推荐
刘悦的技术博客11 天前
MaskGCT,AI语音克隆大模型本地部署(Windows11),基于Python3.11,TTS,文字转语音
ai·tts·python3.11
Python_trys17 天前
python3的语法及入门(近7000字,耐心看包全,看完记得点赞)!
开发语言·python·python3.11
我命由我123451 个月前
1.Python 引入(字面量、注释、变量、数据类型、数据类型转换、标识符、运算符、字符串扩展)
后端·python·学习·pycharm·学习方法·python3.11
我命由我123451 个月前
5.Python 数据容器(list 列表、tuple 元组、str 字符串、set 集合、dict(字典)、序列切片)
数据结构·windows·笔记·python·学习·list·python3.11
技术无疆1 个月前
【Python】Uvicorn:Python 异步 ASGI 服务器详解
运维·服务器·开发语言·网络·python·pygame·python3.11
技术无疆1 个月前
【Python】Arrow使用指南:轻松管理日期与时间
开发语言·人工智能·python·深度学习·机器学习·数据挖掘·python3.11
技术无疆1 个月前
【Python】PythonRobotics:机器人算法的 Python 实现
开发语言·图像处理·python·算法·pycharm·机器人·python3.11
技术无疆1 个月前
阿布量化:基于 Python 的量化交易框架
开发语言·人工智能·python·深度学习·机器学习·数据挖掘·python3.11
技术无疆2 个月前
【Python】Tartiflette:用 Python 实现的 GraphQL 服务器
服务器·开发语言·后端·python·pygame·graphql·python3.11
技术无疆2 个月前
【Python】探索Magenta:音乐与艺术的机器智能创作
开发语言·人工智能·python·scikit-learn·pip·pygame·python3.11