Test if notebook is running on Google Colab

后端 未结 4 1280
小蘑菇
小蘑菇 2021-01-07 19:47

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

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 20:08

    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.

提交回复
热议问题