【问题标题】:How to refer a file in Google Colab when working with Python?使用 Python 时如何在 Google Colab 中引用文件?
【发布时间】:2019-03-11 16:42:23
【问题描述】:

在使用 Google Colab 笔记本时,我必须导出 Google Auth 凭据。

export GOOGLE_APPLICATION_CREDENTIALS='[PATH_TO_CREDS.JSON]'

当我引用像“/Users/user/file”这样的本地文件时,找不到它并得到如下错误。

DefaultCredentialsError:无法自动确定 证书。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式设置 创建凭据并重新运行应用程序

如何使路径正常工作,以便 colab 可以拾取它?

【问题讨论】:

    标签: python google-colaboratory


    【解决方案1】:

    应该有一种方法可以让本地文件工作,但我发现挂载 Google Drive 很方便,而且很管用。我将文件放在 Google Drive 的 Colab Notebooks 文件夹中的临时文件夹中,我保存了所有 Colab 文件。

    from google.colab import drive
    drive.mount('/content/gdrive')
    
    #ensure the file is accessible
    !ls /content/gdrive/'My Drive'/'Colab Notebooks'/temp
    
    import os
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/content/gdrive/My Drive/Colab Notebooks/temp/b08d5871a151.json"
    
    #ensure the path is set correctly
    !echo $GOOGLE_APPLICATION_CREDENTIALS
    

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 2020-08-28
      • 2020-02-27
      • 1970-01-01
      • 2021-01-21
      • 2021-08-02
      • 2021-12-15
      • 2021-05-03
      相关资源
      最近更新 更多