You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

无法导入llama-cpp-python:llama.dll缺失或依赖问题求助

问题描述

尝试安装llama-cpp-python(版本0.3.4,通过匹配Python3.12+AMD64的whl包安装),在Anaconda的JupyterLab中执行import llama_cpp时触发错误:

Could not find module 'D:\anaconda\Lib\site-packages\llama_cpp\lib\llama.dll' (or one of its dependencies)

完整报错堆栈如下:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File D:\anaconda\Lib\site-packages\llama_cpp\_ctypes_extensions.py:67, in load_shared_library(lib_base_name, base_path)
     66 try:
---> 67     return ctypes.CDLL(str(lib_path), **cdll_args)  # type: ignore
     68 except Exception as e:

File D:\anaconda\Lib\ctypes\__init__.py:379, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    378 if handle is None:
---> 379     self._handle = _dlopen(self._name, mode)
    380 else:

FileNotFoundError: Could not find module 'D:\anaconda\Lib\site-packages\llama_cpp\lib\llama.dll' (or one of its dependencies). Try using the full path with constructor syntax.

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[1], line 1
----> 1 import llama_cpp

File D:\anaconda\Lib\site-packages\llama_cpp\__init__.py:1
----> 1 from .llama_cpp import *
      2 from .llama import *
      4 __version__ = "0.3.4"

File D:\anaconda\Lib\site-packages\llama_cpp\llama_cpp.py:38
     36 _base_path = pathlib.Path(os.path.abspath(os.path.dirname(__file__))) / "lib" if _override_base_path is None else pathlib.Path(_override_base_path)
     37 # Load the library
---> 38 _lib = load_shared_library(_lib_base_name, _base_path)
     40 ctypes_function = ctypes_function_for_shared_library(_lib)
     43 # from ggml.h
     44 # // NOTE: always add types at the end of the enum to keep backward compatibility
     45 # enum ggml_type {
   (...)
     76 #     GGML_TYPE_COUNT,
     77 # };

File D:\anaconda\Lib\site-packages\llama_cpp\_ctypes_extensions.py:69, in load_shared_library(lib_base_name, base_path)
     67             return ctypes.CDLL(str(lib_path), **cdll_args)  # type: ignore
     68         except Exception as e:
---> 69             raise RuntimeError(f"Failed to load shared library '{lib_path}': {e}")
     71 raise FileNotFoundError(
     72     f"Shared library with base name '{lib_base_name}' not found"
     73 )

RuntimeError: Failed to load shared library 'D:\anaconda\Lib\site-packages\llama_cpp\lib\llama.dll': Could not find module 'D:\anaconda\Lib\site-packages\llama_cpp\lib\llama.dll' (or one of its dependencies). Try using the full path with constructor syntax.

环境信息:

  • 系统:Windows(AMD64)
  • Python环境:Anaconda,Python3.12
  • 已安装Microsoft Visual C++运行库
  • 仅使用CPU,无GPU
解决方案

1. 检查llama.dll文件完整性

前往路径D:\anaconda\Lib\site-packages\llama_cpp\lib\确认是否存在llama.dll

  • 若文件不存在:说明whl包安装不完整,执行以下命令卸载后重新下载匹配版本的whl包安装:
    pip uninstall -y llama-cpp-python
    
    重新安装时确保whl包的Python版本(cp312)、系统架构(win_amd64)与本地环境完全匹配。

2. 修复Microsoft Visual C++运行库

即使已安装VC++运行库,仍可能存在版本不匹配或安装损坏的情况:

  • 卸载系统中所有版本的Microsoft Visual C++ Redistributable,然后安装最新版的x64位VC++运行库合集包(包含2015-2022版本)。
  • 安装完成后重启电脑,再尝试导入llama_cpp。

3. 配置Anaconda环境的PATH变量

确保llama.dll所在路径被加入系统PATH:

  • 打开Anaconda Prompt,激活目标环境:
    conda activate 你的环境名
    
  • 临时将dll路径加入PATH:
    set PATH=D:\anaconda\Lib\site-packages\llama_cpp\lib;%PATH%
    
  • 在同一个Prompt中启动JupyterLab:
    jupyter lab
    
    若导入成功,可将该路径永久添加到系统环境变量的PATH中。

4. 从源码编译安装

如果whl包始终存在问题,尝试从源码编译:

  • 卸载现有包:
    pip uninstall -y llama-cpp-python
    
  • 设置CPU模式的编译参数:
    set CMAKE_ARGS="-DLLAMA_CUBLAS=OFF"
    set FORCE_CMAKE=1
    
  • 重新安装指定版本:
    pip install llama-cpp-python==0.3.4
    
    编译需要Microsoft Visual Studio的C编译工具链(需安装"Desktop development with C"组件),完成后再测试导入。

5. 排查依赖项缺失

若以上方法无效,使用Dependency Walker工具打开llama.dll,查看是否有缺失的系统库文件,根据提示安装对应缺失组件即可。

内容的提问来源于stack exchange,提问作者PuiHean

火山引擎 最新活动