I work with Google Colab notebooks daily and have to restart them frequently because of runtime limits. Every time I restart, I need to reconnect my Google Drive storage which gets annoying.
Currently I use this setup:
from google.colab import drive
drive.mount('/content/gdrive')
This always shows me an authentication screen asking me to visit a URL and paste back a verification code. Since I’m already logged into my Google account in the same browser, it seems unnecessary to go through this process repeatedly.
Is there a way to make this authentication stick between sessions? I’d prefer not to hardcode any tokens or credentials directly in my notebook for security reasons. Any suggestions for streamlining this workflow would be helpful.
The authentication requirement is built-in security that you can’t fully disable. However, there are alternative approaches you might find useful. Enabling third-party cookies in your browser can sometimes keep you logged in for longer periods between sessions. Additionally, consider modifying your workflow by not mounting Drive immediately. Instead, load essential files into Colab’s local storage at the beginning using commands like !wget for direct links. For frequently used datasets or models, upload them directly to Colab’s file system rather than through Drive. This way, you reduce reliance on Drive for your core activities. You will still need to authenticate when saving outputs back to Drive, but the overall process can be streamlined.
totally get it, miar! it’s super frustrating. you could try keeping your session alive by running some cell every now and then, but i know that’s not ideal. hope they fix this auth issue soon.
Unfortunately, you cannot bypass the Google Drive authentication in Colab permanently. The design ensures the token expires when the runtime disconnects for security. However, you can simplify the process by adding force_remount=False in your mount command. This way, it checks if Drive is already mounted and won’t prompt for authentication again unless necessary. If you frequently use the same files, consider copying them to the local Colab instance using !cp. This allows you to authenticate once per session and use local copies until the runtime ends.