gpt4 book ai didi

python - 从 google.cloud 导入 firestore ModuleNotFoundError : No module named 'google'

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:50 25 4
gpt4 key购买 nike

我无法让我的应用程序从 Google Firestore 读取文档。

这是我的python Flask main.py

from flask import Flask
from google.cloud import firestore

app = Flask(__name__)
db = firestore.Client()

@app.route('/')
def hello():
posts_ref = db.collections(u'posts')
posts = posts_ref.get()
for post in posts:
return u'{} => {}'.format(post.id, post.to_dict())


if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)

但是日志显示以下错误。

2019-02-22 12:33:08 default[2-9]  "GET / HTTP/1.1" 502
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [8] [INFO] Starting gunicorn 19.9.0
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [8] [INFO] Listening at: http://0.0.0.0:8081 (8)
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [8] [INFO] Using worker: threads
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [23] [INFO] Booting worker with pid: 23
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [23] [ERROR] Exception in worker process
2019-02-22 12:33:10 default[2-9] Traceback (most recent call last): File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py",l
ine 583, in spawn_worker worker.init_process() File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104,
in init_process super(ThreadWorker, self).init_process() File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py",li
ne 129, in init_process self.load_wsgi() File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_
wsgi self.wsgi = self.app.wsgi() File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi self.cal
lable = self.load() File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load return self.load_wsgiap
p() File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp return util.import_app(self.app
_uri) File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app __import__(module) File "/srv/main
.py", line 2, in <module> from google.cloud import firestore ModuleNotFoundError: No module named 'google'
2019-02-22 12:33:10 default[2-9] [2019-02-22 12:33:10 +0000] [23] [INFO] Worker exiting (pid: 23)
2019-02-22 12:33:11 default[2-9] [2019-02-22 12:33:11 +0000] [8] [INFO] Shutting down: Master

我的目录结构

enter image description here

我在 lib 文件夹和 google 文件夹中有 init.py。

最佳答案

正如 dustin-ingram@ 所建议的,您可能没有导入 google.cloud.firestore (可能还有 flask )正确打包。

一个好的做法是创建一个 virtualenv,一个 requirements.txt,然后是 pip install -r requirements.txt。其内容可能是:

flask==1.0.2
google-cloud-firestore==0.31.0

您的代码中也有错字;它是 collection 而不是 collections:

posts_ref = db.collection(u'posts')

您需要在您的项目中启用 Firestore(并且可能在您运行代码之前创建 posts 集合)。

关于python - 从 google.cloud 导入 firestore ModuleNotFoundError : No module named 'google' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54827571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com