导入google.cloud.datastore无法正常工作

时间:2017-03-28 08:36:16

标签: python-2.7 google-app-engine google-cloud-datastore

我在pycharm(在windows中)环境中工作。

我正在构建一个网站,以便向数据存储区发送查询并显示值。我还有kind="SpecialTest"下的数据,该数据会通过 Node.js 自动保存。现在我想从Python查询数据。

我从命令行安装了google-cloud-datastore

但我无法建立与数据存储区的连接。

我的代码:

from google.cloud import datastore
cli=datastore.Client(project="My_project_I")
query4= cli.query(kind="SpecialTest").order('Published_at')
print query4

我的结果:

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1272, in default_dispatcher
    self.handlers[handler] = handler = import_string(handler)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1850, in import_string
    return getattr(__import__(module, None, None, [obj]), obj)
  File "C:\Users\Harry\workspace\www\app\account.py", line 11, in <module>
    from google.cloud import datastore
  File "C:\Python27\lib\site-packages\google\cloud\datastore\__init__.py", line 60, in <module>
    from google.cloud.datastore.batch import Batch
  File "C:\Python27\lib\site-packages\google\cloud\datastore\batch.py", line 24, in <module>
    from google.cloud.datastore import helpers
  File "C:\Python27\lib\site-packages\google\cloud\datastore\helpers.py", line 23, in <module>
    from google.protobuf import struct_pb2
ImportStringError: import_string() failed for 'app.account.UserAccount'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Original exception:

ImportError: No module named protobuf

Debugged import:

- 'app' found in 'C:\\Users\\Harry\\workspace\\www\\app\\__init__.pyc'.
- 'app.account' not found.

1 个答案:

答案 0 :(得分:1)

GAE不提供数据存储客户端库,您需要install/vendor it进入您的应用程序(与独立脚本不同的方法)。

或者,您可以使用GAE特定的数据存储库 - 它实际上是推荐的,因为它提供了一些优于通用目的的优势。来自Google App Engine Standard Environment Client Libraries

  

将Cloud Datastore与App Engine标准环境集成   使用App Engine客户端库的应用程序。

     

...

     

Python Google Datastore NDB Client Library

     

注意:对于使用Python编写的App Engine应用程序,不再推荐使用Google Datastore DB Client Library;使用   改为使用Google Datastore NDB客户端库。