Why doesn't the command line show all my print() statements?

While developing I use print() statements (with f-strings) but the not all the output is sent to the console.

When I hit ctrl-c and kill the server - I can see all that is printed but when it’s running I don’t always see every statement that should be printing.

I assume there is some sort of timing issue here - is there a way to force the server that comes with Django to print out all the print() statements?

P.S. Yes I know I should be using logging - but the world is not a perfect place.

I’m guessing you’re using manage.py runserver to run your code during development. If so, I’ve also encountered this in the past.

What has worked for me has been to disable output buffering in the python instance by running manage with the -u parameter. In other words, your command would look something like this:
python -u manage.py runserver

Another similar option would be to set the PYTHONUNBUFFERED environment variable to any non-empty string.

(see https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED)

1 Like

Thank you Ken! I will try that!

For me the python-u manage.py runserver command did not do the trick, but what happens in my case is:

No output:
print(os.getcwd()

Correct output:
print("----", os.getcwd()

I have no explanation why this is the case, but this always does the trick for me.