13

In google colab, I easily mount my google drive with this:

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

In kaggle's notebook, however, it gives this error:

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'

This is my setup in kaggle notebook (also tested this, did not work):

!pip install google-colab # I don't know if this is the correct package
from google.colab import drive
drive.mount('/content/gdrive')
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Kasra
  • 1,959
  • 1
  • 19
  • 29

3 Answers3

12

In fact, the google-colab library does not exist in the Kaggle Kernel. In this way, I use the following procedure to deal with this problem in Kaggle Kernel:

  • First, extract the ID of your desire file from google drive:

    1. In your browser, navigate to drive.google.com.

    2. Right-click on the file, and click "Get a shareable link"

      Right click get shareable link

    3. Then extract the ID of file from URL:

      enter image description here

  • Next, install gdown PyPI module using conda:

    ! conda install -y gdown

  • Finally, download the file using gdown and the intended ID:

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

For example:

!gdown --id 1-1wAx7b-USG0eQwIBVwVDUl3K1_1ReCt

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • is there a way to do the same with folders? i have tried this method with folders on google drive but it does not work I was wondering if there was another way ? – vipul petkar Jun 13 '21 at 14:43
  • @vipulpetkar Are you mean is downloading a file that is into a subfolder or you want to download a folder directly? – Benyamin Jafari Jun 14 '21 at 05:57
  • I want to download an entire drive folder directly into kaggle. I have 10,000 images in google drive folder and i want to send them to kaggle, i dont have any other option than to send and entire folder. – vipul petkar Jun 29 '21 at 16:10
  • @vipulpetkar An alternative way is to create a CloudFare workers (https://workers.cloudflare.com/) to access your files on Google Drive. I believe that would work. You need to dig a bit on Internet to find tutorial on how to do. I remember there is a tool that helps you configure but can't find it in my history – Jean-Francois T. Oct 11 '21 at 06:27
3

google-colab is not maintained by Google, and Colab libraries like drive.mount won't work outside of the Colab environment itself.

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
0

As the stack trace suggests you have some keys missing.

  • CLOUDSDK_CONFIG

You can get this key by logging into GCP and searching for creds

To use these secretively: place this key into Kaggle Secrets Add-ons.

Kayvan Shah
  • 375
  • 2
  • 11
  • Another way to do this try logging the keys and activities from the drive mount script in Google Colab to get that keys – Kayvan Shah Oct 24 '21 at 15:06