[英]from google.cloud import firestore ModuleNotFoundError: No module named 'google'
I am unable to get my application working reading documents from Google Firestore. 我无法让我的应用程序正在阅读Google Firestore中的文档。
Here is my python Flask main.py 这是我的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)
However the logs are showing the following error. 但是日志显示以下错误。
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
My directory structure 我的目录结构
I have init .py in lib folder and in google folder. 我在lib文件夹和google文件夹中有init .py。
As dustin-ingram@ suggests, it's probable that you've not imported the google.cloud.firestore
(and possibly flask
) packages correctly. 正如dustin-ingram @所暗示的那样,你很可能没有正确导入
google.cloud.firestore
(可能还有flask
)软件包。
A good practice is to create a virtualenv, a requirements.txt
and then pip install -r requirements.txt
. 一个好的做法是创建virtualenv,
requirements.txt
然后pip install -r requirements.txt
。 Its contents may be: 其内容可能是:
flask==1.0.2
google-cloud-firestore==0.31.0
You've also a typo in your code; 您的代码中也有拼写错误; it's
collection
not collections
: 它的
collection
不是collections
:
posts_ref = db.collection(u'posts')
You will need to have enabled Firestore in your project (and possibly created the posts
collection before you run your code too). 您需要在项目中启用Firestore(并且可能在运行代码之前创建了
posts
集合)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.