Test if notebook is running on Google Colab

血红的双手。 提交于 2019-12-01 19:00:04

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
Tom Hale

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