【问题标题】:Python tqdm import check if jupyter notebook or lab is runningPython tqdm 导入检查 jupyter notebook 或 lab 是否正在运行
【发布时间】:2018-11-27 16:19:24
【问题描述】:

我有一个模块 (tqdm),我需要根据我是在 jupyter notebook 还是 jupyter lab 环境中运行 .ipynb 来以不同的方式导入它。有没有办法我可以在 python 中确定这一点?例如:

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

【问题讨论】:

标签: python-3.x jupyter-notebook jupyter-lab


【解决方案1】:
# either:
from tqdm.autonotebook import tqdm
# or to suppress the warning:
from tqdm.auto import tqdm

对于其他模块/检查,请参阅How can I check if code is executed in the IPython notebook?(但请注意,尽管不受欢迎,但已接受的答案是“这不是有意设计的。”)

【讨论】:

  • 这里只说,如何导入tqdm,不说如何检测jupyter!
  • OP 专门针对tqdm。如果您想要更通用的测试,请在 OP 的评论中链接不同的答案
  • OP以tqdm为例。这篇文章是否应该重新命名为“如何在 Jupyter 中导入 tqdm”?因为现在它出现在错误的搜索结果中。而不是:stackoverflow.com/questions/15411967/…
  • 嗯是的,看起来像。
  • 更新标题和答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 2021-12-09
  • 2019-08-07
相关资源
最近更新 更多