Python 和 Firebase Firestore 错误无法导入 Cloud Firestore 库

Posted

技术标签:

【中文标题】Python 和 Firebase Firestore 错误无法导入 Cloud Firestore 库【英文标题】:Python and Firebase Firestore error Failed to import the Cloud Firestore library 【发布时间】:2022-01-23 02:01:02 【问题描述】:

我想将我的 Raspberry Pi 4B 连接到 Firebase Firestore。我正在使用 VS Code 和 SSH 在 RPi 上编写代码。我做了所有必要的导入,但出现了这个错误:

Failed to import the Cloud Firestore library for Python. Make sure to install the "google-cloud-firestore" module.

错误与这行代码有关:

from firebase_admin import firestore

当我在本地计算机上使用 PyCharm 并执行相同的导入时,一切顺利。

这是我的代码:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# Use a service account
cred = credentials.Certificate('***.json')
firebase_admin.initialize_app(cred)

【问题讨论】:

【参考方案1】:

您发布的代码完全没问题。其实和文档是一样的,可能是和树莓派有关的东西,并不是说它有问题,而是环境和依赖可能和你本地的不一样,导致它在树莓派中无法运行,但是在您的计算机上工作。您可以通过在两者上运行python3 -m pip freeze 来check the packages 以查看它们是否不同。

如果这无助于识别任何缺失的依赖项,您可以尝试安装所需的包,例如 grcp

pip install grpcio

重新安装“google-cloud-firestore”

pip install google-cloud-core
pip install google-cloud-firestore

或者卸载并重新安装“google-cloud-firestore”

pip uninstall google-cloud-firestore
pip install google-cloud-firestore

正如question 中所建议的那样。

【讨论】:

以上是关于Python 和 Firebase Firestore 错误无法导入 Cloud Firestore 库的主要内容,如果未能解决你的问题,请参考以下文章

相同的 Flutter Web 构建使用 `dhttp` 在本地运行,但使用 `firebase serve` 或 `firebase deploy` 失败

vue-cli v3 与 node.js firebase 函数,firebase 托管(错误:在函数源目录中找不到 npm 包)

孤荷凌寒自学python第五十三天使用python写入和修改Firebase数据库中记录

python Firebase和Gzipped旋转文件记录处理程序

慢响应Firebase + Python + Raspberry pi

Firebase JWT 库无法验证 Python JWT 令牌