Python TQDM导入检查Jupyter Notebook或Lab是否正在运行 [英] Python tqdm import check if jupyter notebook or lab is running

查看:106
本文介绍了Python TQDM导入检查Jupyter Notebook或Lab是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块(tqdm),需要导入该模块,具体取决于我是在jupyter笔记本电脑还是jupyter实验室环境中运行 .ipynb .有什么方法可以在python中确定吗?例如:

I have a module (tqdm) that I need to import differently depending on whether I'm running my .ipynb in a jupyter notebook or jupyter lab environment. Is there way that I can determine this in python? For example:

if <jupyter notebook>:
    from tqdm import tqdm_notebook as tqdm 
elif <jupyter lab>:
    from tqdm import tqdm
else:
    print("Not in jupyter environment.")

推荐答案

# either:
from tqdm.autonotebook import tqdm
# or to suppress the warning:
from tqdm.auto import tqdm

有关其他模块/检查,请参见

For other modules/checks, see How can I check if code is executed in the IPython notebook? (But note that the accepted albeit unpopular answer there is "this is intentionally not meant to be possible by design.")

这篇关于Python TQDM导入检查Jupyter Notebook或Lab是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆