35

I mounted to Google drive account A. Now I want to switch to account B but I cannot do that, because there is no way for me to enter a new authentication key when executing drive.mount().

What I have tried and failed:

  1. restart browser, restart computer
  2. use force_remount=True in drive.mount(), it will only automatically remount account A. Not asking me for new mounting target.
  3. change account A password
  4. change run-time type from GPU to None and back to GPU
  5. open everything in incognito mode
  6. sign out all google accounts

How can I:

  • forget previous authentication key so it will ask me for a new one?
  • dismount drive and forget previous authentication key?

10 Answers 10

46

I have found the 'Restart runtime...' not to work, and changing permission too much of a hassle.
Luckily, the drive module is equipped with just the function you need:

from google.colab import drive
drive.flush_and_unmount()
1
  • Wow thanks that's exactly what I needed for large files
    – JZL003
    Commented Mar 7, 2023 at 13:52
16

You can reset your Colab backend by selecting the 'Reset all runtimes...' item from the Runtime menu.

Be aware, however, that this will discard your current backend.

13

Method 1:

  1. Restart runtime = Resets your Python shell or
  2. Disconnect and delete runtime = to return all managed virtual machines assigned to you to their original state. This can be helpful in cases where a virtual machine has become unhealthy e.g. due to accidental overwrite of system files, or installation of incompatible software. enter image description here

Method 2: terminate your session and run your code again.

Steps:

1) Press "Additional connection options" button and then select "Manage sessions" enter image description here

2) Choose the session you want to terminate and then press the "Terminate" button. enter image description here

4) Run again your code to authenticate yourself:

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

Interesting sources about this topic:

  1. introduction_to_colab_and_python --> Runtime
  2. Runtime > Disconnect and delete runtime
0
12

To force Colab to ask for a new key without waiting or resetting the runtime you can revoke the previous key. To do this:

  1. go to https://myaccount.google.com/permissions (or manually navigate to SecurityManage third-party access on your Google account page),
  2. on the top right, select your profile image or initial, and then select the account whose drive you want do disconnect from Colab,
  3. select Google Drive File Stream in the Google apps section, then select Remove access.

Executing drive.mount() will now ask for a new key.

1
  • After step3 I had to remount forcibly to make it work: drive.mount('/content/gdrive', force_remount=True)
    – hoper
    Commented Aug 17, 2020 at 5:13
4

Remount does not work when you have recently mounted and unmounted using flush_and_unmount(). The correct steps you should follow is (which worked for me at the time of posting):

After mounting using:

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

Unmount using: drive.flush_and_unmount() and you cannot see the 'drive/' folder but TRUST me you should use !rm -rf /content/drive before remounting the drive using:

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

And you will again get the authorise request for a new Gmail account.

1
  • !rm -rf /content/drive was the solution. Without, old state of the Google drive kept appearing which was completely different from the state of the current Google drive contents.
    – mon
    Commented Jul 1, 2021 at 2:50
1

You can terminate the session in Runtime -> manage session. That should do the work and you can remount the drive again.

0

Restarting runtimes and removing access did not help. I discovered that the notebook I was using created directories on the mountpoint:

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

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).

I had to first remove the subdirectories on the mountpoint. First ensure that your drive is not actually mounted!

!find /content/drive

/content/drive
/content/drive/My Drive
/content/drive/My Drive/Colab Notebooks
/content/drive/My Drive/Colab Notebooks/assignment4
/content/drive/My Drive/Colab Notebooks/assignment4/output_dir
/content/drive/My Drive/Colab Notebooks/assignment4/output_dir/2020-04-05_16:17:15

The files and directories above was accidentally created by the notebook before I've mounted the drive. Once you are sure (are you sure?) your drive is not mounted then delete the subdirectories.

!rm -rf /content/drive

After this, I was able to mount the drive.

0
0

Here is an explanation from their FAQs.

Why do Drive operations sometimes fail due to quota? Google Drive enforces various limits, including per-user and per-file operation count and bandwidth quotas. Exceeding these limits will trigger Input/output error as above, and show a notification in the Colab UI. A typical cause is accessing a popular shared file, or accessing too many distinct files too quickly. Workarounds include:

Copy the file using drive.google.com and don't share it widely so that other users don't use up its limits. Avoid making many small I/O reads, instead opting to copy data from Drive to the Colab VM in an archive format (e.g. .zip or.tar.gz files) and unarchive the data locally on the VM instead of in the mounted Drive directory. Wait a day for quota limits to reset.

https://research.google.com/colaboratory/faq.html#drive-quota

0

Symptom

  1. /content/drive gets auto-mounted without mounting it and not being asked for Enter your authorization code:.
  2. Cached old state of the drive kept showing up.
  3. The actual Google drive content did not show up.
  4. Terminating, restarting, factory resetting revoking permissions, clear chrome cache did not work.
  5. Flush and unmount google.colab.drive.flush_and_unmount() did not work.

Solution

  1. Create a dummy file inside the mount point /content/drive.
  2. Take a moment and make sure the content /content/drive is not the same with that in the Google drive UI.
  3. Run rm -rf /content/drive.
  4. Run google.colab.drive.flush_and_unmount()
  5. From the menu Runtime -> Factory reset runtime.

Then re-run google.colab.drive.mount('/content/drive', force_remount=True) finally asked for Enter your authorization code.

-1

The current code for the drive.mount() function is found at https://github.com/googlecolab/colabtools/blob/fe964e0e046c12394bae732eaaeda478bc5fa350/google/colab/drive.py

It is a wrapper for the drive executable found at /opt/google/drive/drive. I have found that executable accepts a flag authorize_new_user which can be used to force a reauthentication.

Copy and paste the contents of the drive.py file into your notebook. Then modify the call to d.sendline() currently on line 189 to look like this (note the addition of the authorize_new_user flag):

d.sendline(
      ('cat {fifo} | head -1 | ( {d}/drive '
       '--features=max_parallel_push_task_instances:10,'
       'max_operation_batch_size:15,opendir_timeout_ms:{timeout_ms},'
       'virtual_folders:true '
       '--authorize_new_user=True '
       '--inet_family=' + inet_family + ' ' + metadata_auth_arg +
       '--preferences=trusted_root_certs_file_path:'
       '{d}/roots.pem,mount_point_path:{mnt} --console_auth 2>&1 '
       '| grep --line-buffered -E "{oauth_prompt}|{problem_and_stopped}"; '
       'echo "{drive_exited}"; ) &').format(
           d=drive_dir,
           timeout_ms=timeout_ms,
           mnt=mountpoint,
           fifo=fifo,
           oauth_prompt=oauth_prompt,
           problem_and_stopped=problem_and_stopped,
           drive_exited=drive_exited))

Call either the drive module version of flush_and_unmount() or the one you pasted in, and then call your version of mount() to login as a different user!

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