How can I test if my notebook is running on Google Colab?
I need this test as obtaining / unzipping my training data is different if running on my laptop or on Colab.
Google's Colab allows anyone to write and share Jupyter Notebooks online from anywhere. I've used Google Colab quite a lot. It used to be my default Jupyter Notebook environment and the starting point for many of my articles on Medium (I also use VSCode these days, too, but that is another story).
Colab notebooks are stored in Google Drive, or can be loaded from GitHub. Colab notebooks can be shared just as you would with Google Docs or Sheets. Simply click the Share button at the top right of any Colab notebook, or follow these Google Drive file sharing instructions.
Colaboratory lets you connect to a local runtime using Jupyter. This allows you to execute code on your local hardware and have access to your local file system.
Stop Colab from Disconnecting In fact, if we leave the notebook idle for more than 30 minutes, Google Colab automatically disconnects it. Every 60 seconds, this function clicks the connect button. As a result, Colab believes that the notebook is not idle, and you should not be concerned about being disconnected!
There is also the possibility to check the ipython interpreter used. I think it is a little bit more clear and you don't have to import any module.
if 'google.colab' in str(get_ipython()): print('Running on CoLab') else: print('Not running on CoLab')
If you need to do it multiple times you might want to assign a variable so you don't have to repeat the str(get_ipython())
.
RunningInCOLAB = 'google.colab' in str(get_ipython())
RunningInCOLAB
is True if run in a Google Colab notebook.
Try importing google.colab
try: import google.colab IN_COLAB = True except: IN_COLAB = False
Or just check if it's in sys.modules
import sys IN_COLAB = 'google.colab' in sys.modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With