44

I'm looking for a way to automate the authentication process when connecting a colab-session to my google drive.

I'd prefer to use the built-in tools for this one, instead of PyDrive.

In short: have the following cell run without having to manually authenticate by logging in and copying the password from the dialogue

from google.colab import drive
drive.mount('/content/drive/')
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
Marius Brataas
  • 614
  • 1
  • 5
  • 8
  • Can't you use a browser extension? like:https://chrome.google.com/webstore/detail/colab-auto-reconnect/nbcihfbfamjlfiopdcemmohoojdecjid?hl=en – Lupos Dec 02 '19 at 17:16
  • I've coded this previously but no longer have access to the code. In short, you use Beautiful Soup to capture the auth code that Google provides. This saves you from having to manually copy and paste it and allows automatic authentication. https://towardsdatascience.com/in-10-minutes-web-scraping-with-beautiful-soup-and-selenium-for-data-professionals-8de169d36319 – Liquidgenius Jan 11 '20 at 17:16

1 Answers1

55

Automatically mounting to your Drive files is now supported for Colab notebooks which aren't shared by multiple people.

To enable this for a notebook, create a new Drive notebook, open the file browser, and click the 'Mount Drive' button.

enter image description here

You'll see a permissions dialog like so:

enter image description here

After you complete the permissions once, you'll see your Drive mounted in the file browser.

enter image description here

Better still, if you reload the notebook later and reconnect, your Drive will mount automatically with no more drive.mount copy/paste required. Your Drive files will just be there.

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 6
    Is this a colab pro specific feature ? Because I can not find this Mount Drive button from file menu. – Thava May 27 '20 at 12:20
  • This method appears to be working. I can still load files from Drive and closing and re-opening the notebook. – JaredS May 30 '20 at 14:12
  • 3
    @Thava i have colab pro and i also didn't have this activated in my older notebooks (just the activation code cell is added when i click on the button). Now i tried with a new notebook and it works as Bob says :) – cookiemonster Jun 12 '20 at 13:20
  • 15
    one gotcha: if you import a notebook, Colab won't let you use the built-in mounting feature. My workaround is to create a blank notebook, mount Drive, import a notebook in a new window, then copy and paste the cells to the original one. – philipkd Sep 28 '20 at 23:06
  • 11
    Ctrl-Shift-A/Ctrl-V works for copy-and-paste all cells at one go. It took me a while to figure it out. I hope this will help someone in the future. – mikey Oct 16 '20 at 11:30
  • 2
    Working with free colab, Nov 2020 – leetbacoon Nov 27 '20 at 22:42
  • @mikey in Chrome, Ctrl+Shift+A opens tab search now( – Osman-pasha Apr 22 '21 at 13:08
  • To copy all cells, you can click in the header of your first cell, hold shift, and down arrow key your way to all cells, and then ctrl+c copy. – tobek Oct 09 '21 at 19:48
  • Is there a way to undo this? I.e. if i want to auth against a different google drive under another account I have access to. – kawingkelvin Jul 04 '22 at 21:40
  • 2
    Note working. When you click on the "Google Drive icon", it just create two lines of code in the notebook "from google.colab import drive drive.mount('/content/drive')". Every time the runtime is disconnected, we need to mount the drive again and give authentication – – High GPA Aug 08 '22 at 00:01
  • What if I'm working with a colab notebook that is shared with many people? Is there any way around this? – seeker_after_truth Sep 19 '22 at 15:56