Test if notebook is running on Google Colab

不羁岁月 提交于 2019-12-01 16:22:39

问题


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.


回答1:


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



回答2:


In a %%bash cell, use:

%%bash
[[ ! -e /colabtools ]] && exit  # Continue only if running on Google Colab

# Do Colab-only stuff here

Or in Python equivalence

import os
if os.path.exists('/colabtools'):
  # do stuff


来源:https://stackoverflow.com/questions/53581278/test-if-notebook-is-running-on-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!