调用另一个脚本时,日志输出未显示在 jupyter 中

Log outputs are not shown in jupyter when calling another script

我有一个 Jupyter 笔记本 (.ipynb),其中包含以下内容:

%%bash

python log_test.py

其中 log_test.py 是:

import time

print('this is first appearance...')

time.sleep(10)

print('after 10 sec sleep')

time.sleep(10)

print('after 10 sec sleep') 

当我执行 jupyter 单元时,它不会在 print() 函数后直接显示输出,而是等到作业完全完成。我想知道是否可以通过某种方式立即显示输出(无需等待 20 秒)?

所以我关注了这个website

因此我替换了代码

%%bash
python log_test.py

这一行:

%run -i log_test.py

实时打印出来