在kaggle笔记本电脑中安装Google驱动器

时间:2020-01-13 11:00:55

标签: python jupyter-notebook google-drive-api google-colaboratory kaggle

在google colab中,我可以通过以下方式轻松安装google驱动器:

from google.colab import drive
drive.mount('/content/gdrive')

但是,在kaggle的笔记本中,会出现此错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-14-2b128295b616> in <module>
      2 # !pip install google-colab
      3 from google.colab import drive
----> 4 drive.mount('/content/gdrive')
      5 # Set your own project id here
      6 # PROJECT_ID = 'your-google-cloud-project'

/opt/conda/lib/python3.6/site-packages/google/colab/drive.py in mount(mountpoint, force_remount, timeout_ms)
     80     return
     81 
---> 82   env = _env()
     83   home = env.home
     84   root_dir = env.root_dir

/opt/conda/lib/python3.6/site-packages/google/colab/drive.py in _env()
     41   home = _os.environ['HOME']
     42   root_dir = _os.path.realpath(
---> 43       _os.path.join(_os.environ['CLOUDSDK_CONFIG'], '../..'))
     44   inet_family = 'IPV4_ONLY'
     45   dev = '/dev/fuse'

/opt/conda/lib/python3.6/os.py in __getitem__(self, key)
    667         except KeyError:
    668             # raise KeyError with the original key value
--> 669             raise KeyError(key) from None
    670         return self.decodevalue(value)
    671 

KeyError: 'CLOUDSDK_CONFIG'

这是我在kaggle笔记本中的设置(也经过this测试,无法正常工作):

!pip install google-colab # I don't know if this is the correct package
from google.colab import drive
drive.mount('/content/gdrive')

2 个答案:

答案 0 :(得分:1)

google-colab不是Google维护的,像drive.mount这样的Colab库不能在Colab环境本身之外工作。

答案 1 :(得分:0)

事实上,google-colab库在Kaggle内核中不存在。这样,我使用以下过程来解决Kaggle内核中的此问题:

  • 首先,从Google驱动器中提取所需文件的ID:

    1. 在浏览器中,导航至drive.google.com。

    2. 右键单击文件,然后单击“获取可共享的链接”

      Right click get shareable link

    3. 然后从URL中提取文件ID:

      enter image description here

  • 接下来,使用gdown安装conda PyPI模块:

    ! conda install -y gdown

  • 最后,使用gdown和预期的ID下载文件:

    !gdown --id <put-the-ID>

例如:

!gdown --id 1-1wAx7b-USG0eQwIBVwVDUl3K1_1ReCt