测试笔记本是否在Google Colab上运行 [英] Test if notebook is running on Google Colab
问题描述
如何测试我的笔记本是否在Google Colab上运行?
How can I test if my notebook is running on Google Colab?
我需要进行此测试,因为在便携式计算机或Colab上运行时,获取/解压缩训练数据的方式是不同的.
I need this test as obtaining / unzipping my training data is different if running on my laptop or on Colab.
推荐答案
还可以检查使用的ipython解释器.我认为这更加清楚了,您不必导入任何模块.
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')
如果您需要多次执行此操作,则可能要分配一个变量,这样就不必重复str(get_ipython())
.
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())
如果在Google Colab笔记本中运行,
RunningInCOLAB
为True.
RunningInCOLAB
is True if run in a Google Colab notebook.
这篇关于测试笔记本是否在Google Colab上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!