11
$\begingroup$

I have recently discovered Google Colab and I am wondering if there is an option to permanently authorize Google Colab to access and mount my Google Drive.

from google.colab import drive
drive.mount('/content/drive')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=xxx
Enter your authorization code:
··········
Mounted at /content/drive

Right now, I need to all the time insert manually the authorization code and I want when I start a new Google Colab to have my drive mounted.

Is there an option to do that or I should execute this all the time manually?

$\endgroup$

3 Answers 3

2
$\begingroup$
  1. Choose the folder icon in the left sidebar.
  2. Wait for the runtime to initialize (you will see icons appear under the word "Files" in the sidebar).
  3. Choose the Google Drive icon (the hover text is "Mount Drive").

Using this method, rather than messing up your notebook with the google.colab package, is nice and Colab maintains the authorization until you "Unmount Drive" using the crossed out Google Drive icon.

$\endgroup$
3
  • 1
    $\begingroup$ Your method is still a colab package. 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 $\endgroup$
    – High GPA
    Aug 7, 2022 at 23:58
  • 2
    $\begingroup$ @HighGPA I've noticed that behavior too, but only on uploaded .ipynb files. If you create a new Colab document from drive, no lines are inserted with this method. I can't find any other difference between uploaded and new Colab documents (or a way to "convert" an uploaded file)! $\endgroup$
    – Ian
    Aug 8, 2022 at 1:12
  • $\begingroup$ You are definitely right. I am trying to find a solution too. It seems like that colab keep a hidden tag of the "original creator" somehow. $\endgroup$
    – High GPA
    Aug 8, 2022 at 1:14
6
$\begingroup$

For some reason, Colab allows me to automatically mount if I'm the creator of the notebook. If that's not the case, such was when I create a copy, the from google.colab import drive code block automatically gets added, and I have to authorize again.

File > New notebook

Then navigate to the file browser. Click on "Mount Drive".

From then on, every time I connect, it automatically mounts Drive during the Initializing phase.

$\endgroup$
2
$\begingroup$

I was working on a google colab project, after a while I decided to copy the work and rename it as a new version (manual type of git control). After doing this I noticed that my google drive doesn't automatically mount. After checking some of the settings I went to File > Update Drive Preview after doing this, the google drive is automatically added when restarting the notebook. Even when commenting out these lines...

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

Hope this helps

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.