google cloud libs没有在Colaboratory上工作

时间:2017-12-02 00:01:15

标签: python google-cloud-storage google-colaboratory

我想测试一些与我今天在本地运行的GCS交互的代码。我的计划是:

  1. 在GDrive中保存服务帐户JSON文件
  2. 将其下载到Colaboratory的'local'/ tmp
  3. !export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
  4. storage_client = storage.Client('my-project') 并使用不同的方法/属性。
  5. 我安装了!pip install --upgrade google-cloud,但在测试项目符号4(期待身份验证错误)时,我收到了一条消息依赖项问题:

    ContextualVersionConflictTraceback (most recent call last)
    <ipython-input-13-7346ac058842> in <module>()
    ----> 1 from google.cloud import storage
      2 
      3 
      4 # Authenticate to GCS.
      5 from google.colab import auth
    
    /usr/local/lib/python2.7/dist-packages/google/cloud/storage/__init__.py in <module>()
     33 
     34 from pkg_resources import get_distribution
    ---> 35 __version__ = get_distribution('google-cloud-storage').version
     36 
     37 from google.cloud.storage.batch import Batch
    
    /usr/lib/python2.7/dist-packages/pkg_resources/__init__.pyc in get_distribution(dist)
    554         dist = Requirement.parse(dist)
    555     if isinstance(dist, Requirement):
    --> 556         dist = get_provider(dist)
    557     if not isinstance(dist, Distribution):
    558         raise TypeError("Expected string, Requirement, or Distribution", dist)
    
    /usr/lib/python2.7/dist-packages/pkg_resources/__init__.pyc in get_provider(moduleOrReq)
    428     """Return an IResourceProvider for the named module or requirement"""
    429     if isinstance(moduleOrReq, Requirement):
    --> 430         return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
    431     try:
    432         module = sys.modules[moduleOrReq]
    
    /usr/lib/python2.7/dist-packages/pkg_resources/__init__.pyc in require(self, *requirements)
    970         included, even if they were already activated in this working set.
    971         """
    --> 972         needed = self.resolve(parse_requirements(requirements))
    973 
    974         for dist in needed:
    
    /usr/lib/python2.7/dist-packages/pkg_resources/__init__.pyc in resolve(self, requirements, env, installer, replace_conflicting, extras)
    861                 # Oops, the "best" so far conflicts with a dependency
    862                 dependent_req = required_by[req]
    --> 863                 raise VersionConflict(dist, req).with_context(dependent_req)
    864 
    865             # push the new requirements onto the stack
    
    ContextualVersionConflict: (google-cloud-core 0.27.1 (/usr/local/lib/python2.7/dist-packages), Requirement.parse('google-cloud-core<0.29dev,>=0.28.0'), set(['google-cloud-storage']))
    

    我没有尝试使用Datalab,但是Colaboratory非常方便。

2 个答案:

答案 0 :(得分:3)

所以这里有两个问题/问题:

  1. 我可以使用服务帐户凭据吗?我怀疑事情会起作用,但你会先被其他东西绊倒。

  2. 为什么导入google.cloud.storage时出错?此处的问题如下:我们在启动时加载了google.cloud个库的一些子集。然后进行升级,并尝试加载新安装的库,这些库与已加载的库冲突。

    最快的解决方法是在安装后重新启动内核(ctrl-m .),此时应该正常加载。

答案 1 :(得分:0)

是否需要服务帐户?您可以使用I/O example notebook for GCS中显示的配方直接在Colab中使用您的个人凭据。

Colab VM不是共享的,因此关于项目中共享的GCE VM上的用户凭据的典型警告不适用。

如果您需要使用GCS存储Python API的服务帐户,请告诉我,我会做一个例子。