You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入torch_geometric时pyg-lib与torch-sparse报错求助

PyTorch Geometric导入警告:libpython3.10.dylib文件缺失

每次执行from torch_geometric.nn import MessagePassing时,都会触发关于pyg-lib和torch-sparse导入失败的警告,具体栈信息如下:

/Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/torch_geometric/typing.py:18: UserWarning: An issue occurred while importing 'pyg-lib'. Disabling its usage. Stacktrace: dlopen(/Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/libpyg.so, 0x0006): Library not loaded: /Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib
Referenced from: <17EC4CAB-AF50-3E98-8969-F2498B67D840> /Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/libpyg.so
Reason: tried: '/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/usr/local/lib/libpython3.10.dylib' (no such file), '/usr/lib/libpython3.10.dylib' (no such file, not in dyld cache)
warnings.warn(f"An issue occurred while importing 'pyg-lib'. "
/Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/torch_geometric/typing.py:42: UserWarning: An issue occurred while importing 'torch-sparse'. Disabling its usage. Stacktrace: dlopen(/Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/libpyg.so, 0x0006): Library not loaded: /Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib
Referenced from: <17EC4CAB-AF50-3E98-8969-F2498B67D840> /Users/karan_bania/anaconda3/envs/PyGtut/lib/python3.10/site-packages/libpyg.so
Reason: tried: '/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib' (no such file), '/usr/local/lib/libpython3.10.dylib' (no such file), '/usr/lib/libpython3.10.dylib' (no such file, not in dyld cache)
warnings.warn(f"An issue occurred while importing 'torch-sparse'. "

已尝试的排查步骤

  • 多次使用conda和pip重新安装PyTorch Geometric,问题未解决
  • 排查多Python版本冲突:执行conda list python -n PyGtut确认当前环境仅安装Python 3.10.8,输出如下:
packages in environment at /Users/karan_bania/anaconda3/envs/PyGtut:
Name                      Version            Build Channel 
ipython                   8.12.0             py310hecd8cb5_0
ipython_genutils          0.2.0              pyhd3eb1b0_1
python                    3.10.8               h218abb5_1
python-dateutil           2.8.2              pyhd3eb1b0_0
python-fastjsonschema     2.16.2          py310hecd8cb5_0
python-json-logger        2.0.7           py310hecd8cb5_0
python-lsp-black          1.2.1           py310hecd8cb5_0
python-lsp-jsonrpc        1.0.0              pyhd3eb1b0_0
python-lsp-server         1.7.2           py310hecd8cb5_0
python-slugify            5.0.2              pyhd3eb1b0_0
python.app                3               py310hca72f7f_0
  • 将Python从3.11.1降级到3.10.8,匹配错误提示中的版本,问题依旧
  • 推测是包的安装路径存在问题,但无法确定具体解决方法

环境信息

  • 系统:macOS
  • PyTorch版本:2.0.0

解决方法尝试

  1. 从源码编译安装pyg依赖库
    预编译的libpyg.so依赖CI环境中的Python路径,和本地路径不匹配,直接从源码编译可解决路径问题:

    • 先卸载现有相关包:
      pip uninstall -y torch_geometric pyg-lib torch-sparse torch-scatter torch-cluster torch-spline-conv
      
    • 安装编译依赖(macOS需先安装Xcode命令行工具:xcode-select --install)
    • 从源码安装:
      pip install torch_geometric
      pip install pyg-lib torch-sparse torch-scatter torch-cluster torch-spline-conv --no-binary :all:
      
  2. 设置DYLD_LIBRARY_PATH指向本地Python库
    找到当前conda环境的Python lib路径(示例为/Users/karan_bania/anaconda3/envs/PyGtut/lib),运行脚本前执行:

    export DYLD_LIBRARY_PATH=/Users/karan_bania/anaconda3/envs/PyGtut/lib:$DYLD_LIBRARY_PATH
    

    也可以把该命令添加到conda环境的激活脚本中(/Users/karan_bania/anaconda3/envs/PyGtut/etc/conda/activate.d/env_vars.sh),每次激活环境自动生效。

  3. 创建软链接映射缺失的库路径
    先创建错误提示中的目录,再把本地的libpython3.10.dylib链接到该目录:

    mkdir -p /Users/runner/hostedtoolcache/Python/3.10.8/x64/lib
    ln -s /Users/karan_bania/anaconda3/envs/PyGtut/lib/libpython3.10.dylib /Users/runner/hostedtoolcache/Python/3.10.8/x64/lib/libpython3.10.dylib
    

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:57:26