Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if notebook is running on Google Colab

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.

like image 628
Tom Hale Avatar asked Dec 02 '18 14:12

Tom Hale


People also ask

Does Google colab run Jupyter Notebook?

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).

Where is Google colab running?

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.

Is Google colab running locally?

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.

Does colab keep running?

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!


2 Answers

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.

like image 22
G M Avatar answered Oct 03 '22 23:10

G M


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 
like image 150
korakot Avatar answered Oct 04 '22 01:10

korakot