使用 Python 时如何在 Google Colab 中引用文件?
How to refer a file in Google Colab when working with Python?
在 Google Colab 笔记本上工作时,我必须导出 Google Auth 凭据。
export GOOGLE_APPLICATION_CREDENTIALS='[PATH_TO_CREDS.JSON]'
当我引用像“/Users/user/file”这样的本地文件时,无法找到它并得到如下错误。
DefaultCredentialsError: Could not automatically determine
credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly
create credential and re-run the application
如何使该路径生效,以便 colab 可以使用它?
应该有办法让本地文件工作,但我发现安装 Google 驱动器很方便,而且工作正常。我将文件放在 Google 驱动器的 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
在 Google Colab 笔记本上工作时,我必须导出 Google Auth 凭据。
export GOOGLE_APPLICATION_CREDENTIALS='[PATH_TO_CREDS.JSON]'
当我引用像“/Users/user/file”这样的本地文件时,无法找到它并得到如下错误。
DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credential and re-run the application
如何使该路径生效,以便 colab 可以使用它?
应该有办法让本地文件工作,但我发现安装 Google 驱动器很方便,而且工作正常。我将文件放在 Google 驱动器的 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