rasbt / pyprind

PyPrind - Python Progress Indicator Utility
BSD 3-Clause "New" or "Revised" License
549 stars 65 forks source link

Regression: Redirecting output fails #39

Closed parchd-1 closed 6 years ago

parchd-1 commented 7 years ago

In pyprind 2.9.8 it was possible to redirect stdout to a file and still see the progress from pyprind. Since 2.9.9, the progress doesn't show when redirecting stdout. Instead, a message is output to stdout:

Warning: No valid output stream.

I have only just upgraded to 2.10 directly from 2.9.8, so I hadn't noticed this before.

The problem can be seen by simply running the following as python pyprind_bug.py > output_file

from time import sleep
from pyprind import prog_bar

for i in prog_bar(range(5)):
    sleep(1)
rasbt commented 7 years ago

Hi there, yeah, somehow the automatic output detection does not work reliably on some machines anymore -- not sure what it is, but I have to look at it some time to see if there's a better way to do it ...

In the meantime, what should always work, is setting the stderror stream for pyprind manually, so that you can redirect the stdoutput (e.g,. as produced from your program) to a log file. For example, if your script looks like that:

import pyprind
import sys

pbar = pyprind.ProgBar(10, stream=sys.stderr)
for i in range(10):
    print('hello')
    pbar.update()

then you can run it as

python somescript.py > scriptoutput.txt

This should show the progressbar on the command line but redirect the program's output (here 'hello') to the file.

SimbaMupfu commented 6 years ago

Thanks hey. your answer to the above comment worked perfect on my machine

rasbt commented 6 years ago

Thanks for the note, I think we can close this issue now :)