运行dbt run时出现ModuleNotFoundError: No module named 'google'错误求助
dbt run 执行时触发 ModuleNotFoundError: No module named 'google' 问题
错误日志
Traceback (most recent call last): File "/opt/homebrew/bin/dbt", line 5, in <module> from dbt.cli.main import cli File "/opt/homebrew/lib/python3.11/site-packages/dbt/cli/__init__.py", line 1, in <module> from .main import cli as dbt_cli # noqa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/dbt/cli/main.py", line 14, in <module> from dbt.cli import requires, params as p File "/opt/homebrew/lib/python3.11/site-packages/dbt/cli/requires.py", line 1, in <module> import dbt.tracking File "/opt/homebrew/lib/python3.11/site-packages/dbt/tracking.py", line 15, in <module> from dbt import version as dbt_version File "/opt/homebrew/lib/python3.11/site-packages/dbt/version.py", line 11, in <module> import dbt.semver File "/opt/homebrew/lib/python3.11/site-packages/dbt/semver.py", line 6, in <module> import dbt.utils File "/opt/homebrew/lib/python3.11/site-packages/dbt/utils.py", line 20, in <module> from dbt.events.types import RetryExternalCall, RecordRetryException File "/opt/homebrew/lib/python3.11/site-packages/dbt/events/__init__.py", line 1, in <module> from .adapter_endpoint import AdapterLogger # noqa: F401 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/dbt/events/adapter_endpoint.py", line 3, in <module> from dbt.events.functions import fire_event, EVENT_MANAGER File "/opt/homebrew/lib/python3.11/site-packages/dbt/events/functions.py", line 2, in <module> from dbt.events.base_types import BaseEvent, EventLevel, EventMsg File "/opt/homebrew/lib/python3.11/site-packages/dbt/events/base_types.py", line 4, in <module> from dbt.events import types_pb2 File "/opt/homebrew/lib/python3.11/site-packages/dbt/events/types_pb2.py", line 5, in <module> from google.protobuf.internal import builder as _builder ModuleNotFoundError: No module named 'google'
问题背景
该dbt项目此前可正常运行,创建新环境后出现此问题。已尝试卸载重装dbt、dbt-postgres及google相关模块。
当前环境相关包版本
db-dtypes 1.2.0 dbt 1.0.0.38.12 dbt-adapters 1.1.1 dbt-common 1.0.4 dbt-core 1.8.0 dbt-extractor 0.5.1 dbt-postgres 1.8.2 dbt-semantic-interfaces 0.5.1 google 3.0.0 google-api-core 2.19.0 google-auth 2.29.0 google-cloud-bigquery 3.23.1 google-cloud-core 2.4.1 google-cloud-dataproc 5.9.3 google-cloud-storage 2.16.0 google-crc32c 1.5.0 google-resumable-media 2.7.0 googleapis-common-protos 1.63.0
解决方案
1. 安装缺失的protobuf库
错误提示找不到的google.protobuf属于protobuf包,而非你安装的google(3.0.0是过时的第三方包)。执行以下命令安装:
pip install protobuf
2. 卸载冲突的google包
你环境中的google包是一个非官方的过时包,可能与Google官方的protobuf等库产生命名冲突,卸载它:
pip uninstall -y google
3. 修复dbt版本混乱问题
当前环境中dbt版本(1.0.0.38.12)与dbt-core(1.8.0)、dbt-postgres(1.8.2)版本不匹配,导致依赖混乱。强制重新安装对应版本的dbt-postgres,自动拉取兼容的核心依赖:
pip install --force-reinstall dbt-postgres==1.8.2
4. 重建干净的虚拟环境(终极方案)
如果上述操作仍无法解决,直接创建全新的虚拟环境,仅安装所需的dbt-postgres,避免旧环境残留的依赖冲突:
# 创建并激活虚拟环境(以venv为例) python -m venv dbt-env source dbt-env/bin/activate # macOS/Linux # dbt-env\Scripts\activate # Windows # 安装指定版本的dbt-postgres pip install dbt-postgres==1.8.2
内容的提问来源于stack exchange,提问作者Joseph Tummon
相关产品推荐
相关产品推荐

