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

导入gspread遇ModuleNotFoundError: No module named '_cffi_backend'如何解决?

问题描述

之前能正常导入gspread模块,本周开始导入失败。执行以下代码:

import sys
sys.path.append("/usr/local/lib/python3.10/site-packages")
import gspread

返回错误:ModuleNotFoundError: No module named '_cffi_backend',尽管已安装cffi模块。完整错误栈如下:

ModuleNotFoundError: No module named '_cffi_backend'
thread '<unnamed>' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.20.2/src/err/mod.rs:788:5:
Python API call failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/Users/hamident0/test/test.py", line 4, in <module>
    import gspread
  File "/usr/local/lib/python3.10/site-packages/gspread/__init__.py", line 7, in <module>
    from .auth import (
  File "/usr/local/lib/python3.10/site-packages/gspread/auth.py", line 15, in <module>
    from google.oauth2.credentials import Credentials as OAuthCredentials
  File "/usr/local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 44, in <module>
    from google.oauth2 import reauth
  File "/usr/local/lib/python3.10/site-packages/google/oauth2/reauth.py", line 40, in <module>
    from google.oauth2 import _client
  File "/usr/local/lib/python3.10/site-packages/google/oauth2/_client.py", line 35, in <module>
    from google.auth import jwt
  File "/usr/local/lib/python3.10/site-packages/google/auth/jwt.py", line 57, in <module>
    from google.auth import _service_account_info
  File "/usr/local/lib/python3.10/site-packages/google/auth/_service_account_info.py", line 22, in <module>
    from google.auth import crypt
  File "/usr/local/lib/python3.10/site-packages/google/auth/crypt/__init__.py", line 43, in <module>
    from google.auth.crypt import rsa
  File "/usr/local/lib/python3.10/site-packages/google/auth/crypt/rsa.py", line 20, in <module>
    from google.auth.crypt import _cryptography_rsa
  File "/usr/local/lib/python3.10/site-packages/google/auth/crypt/_cryptography_rsa.py", line 22, in <module>
    import cryptography.exceptions
  File "/usr/local/lib/python3.10/site-packages/cryptography/exceptions.py", line 9, in <module>
    from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions
pyo3_runtime.PanicException: Python API call failed

已删除并重装gspread、cffi等相关包,且通过pip3 show cffi确认cffi位于/usr/local/lib/python3.10/site-packages路径下,需解决导入gspread的问题。

解决方法

1. 重新编译cffi模块

_cffi_backend是cffi的编译后扩展模块,可能因编译缓存或环境问题损坏,执行以下命令强制重新编译:

pip3 install --force-reinstall --no-binary :all: cffi

2. 检查Python环境一致性

确保运行脚本的Python解释器和安装cffi的pip属于同一环境:

  • 查看当前Python路径:
    which python3
    
  • 查看pip3对应的Python路径:
    pip3 -V
    

如果路径不一致,使用对应环境的pip重新安装:

/usr/local/bin/python3 -m pip install --force-reinstall cffi gspread

3. 降级cryptography模块

错误栈显示问题出在cryptography的Rust绑定部分,最新版本可能与Python 3.10或系统环境不兼容,尝试降级到稳定版本:

pip3 install cryptography==41.0.7

4. 清理Python缓存

删除Python的缓存文件,避免缓存干扰:

find /usr/local/lib/python3.10/site-packages -name "__pycache__" -exec rm -rf {} +
find /usr/local/lib/python3.10/site-packages -name "*.pyo" -o -name "*.pyc" | xargs rm -f

5. 移除手动添加的sys.path

尝试去掉代码中手动添加的sys.path.append,避免路径冲突,直接导入:

import gspread

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:55:01