Issue
Example, I have this code saved as test.py
import time
for i in range(0, 10):
print(i)
time.sleep(3.0)
When I try to run this on ipynb with !python test.py
It processed but it doesn't show realtime in output.
I want it to be like show 0, then pause 3 seconds and then show 1 .... etc. in output.
Solution
You may want to try using %run test.py
or %run test.py -i
in a cell in your notebook.
(See here.)
!
sends what you put after it off to a separate temporary shell instance. That instance runs what you sent as process entirely and then upon completion (depending on system) brings the output back to the notebook & closes itself up and cleans up everything.
Actually how it handles things like this can differ on different systems. For example in sessions launched from here by clicking launch binder
(select 'Help' > 'Launch Classic Notebook' if you prefer the other interface although the interface choice makes no difference as both behaved same with your code there), I see !python test.py
works as you seek. However, in general it is best not run scripts in notebooks that way as the magic %run
command is best used as it is more full-featured and uses the environment in which your notebook is served. So on top of maybe not returning output until the end, !python
won't necessarily use the same environment.
Answered By - Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.