0

I just created a new environment with Python 3.10, and I had a piece of code that runs different algorithms, and prints which one is working on in the same line, until it moves to a new product. It was working fine with Python 3.9.12.

The code is quite simple:

print('Product '+ prod + ' - algorithm: ', end='')
# some code
print('Arima ', end='')
# some code running Arima
print('Prophet ', end='')
# some code running Prophet
print('')

The result was something like:

Product A - algorithm:

and a few seconds later:

Product A - algorithm: Arima

and a few seconds later:

Product A - algorithm: Arima Prophet

(just adding 'Prophet' to the previous line)

and a few seconds later, in a new line:

Product B - algorithm: Arima

Now, with Python 3.10.6, I don't see anything until the fourth 'print' command occurs, so it takes a few seconds without any printing, and then it prints all together after all code above has run.

It was very useful to see which algorithm the code was in (I have 5 algorithms), rather than having to wait until all 5 are done to see the printed line in the terminal.

Any clue why Python 3.10.6 would have changed the behaviour? By the way, if I run it in debug mode, then the printing happens as expected! Go figure...

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    It might be a buffering issue. Try flushing stdout after the `print` calls that don't end with a newline. – Tom Karzes Nov 01 '22 at 21:46
  • 2
    I tried checking for myself if 3.9 and 3.10 behave any differently, but they don't seem to. I don't think this is the type of thing that would change between minor versions. What might actually be happening is a difference in your environments, like maybe `PYTHONUNBUFFERED` was set in the old one, or your code runner was configured to use `python -u`. – wjandrea Nov 01 '22 at 21:54

0 Answers0