How to fix pyinvoke run() delayed output

Problem

You are using Invoke aka pyinvoke to run shell commands, but the output is delayed: All output will eventually be printed but not exactly at the time when it is generated by the command being run.

Example:

from invoke import run

run("python myscript.py")

Solution

Use pty=True:

run("python myscript.py", pty=True)

This will stream the output of the python myscript.py in real-time.