The Python Oracle

Flask doesn't print to console

This video explains
Flask doesn't print to console

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Question
01:24 Accepted answer (Score 81)
01:58 Answer 2 (Score 58)
02:27 Answer 3 (Score 19)
02:50 Answer 4 (Score 11)
03:05 Thank you

--

Full question
https://stackoverflow.com/questions/4440...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #flask

#avk47



ACCEPTED ANSWER

Score 88


Try this and see if it helps:

For python2:

from __future__ import print_function
import sys

print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)

For python3 you don't need to import from future print_function:

import sys

print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)

See if it helps to print to console.




ANSWER 2

Score 83


You can force to flush stdout directly from print:

print('enter getJSONReuslt', flush=True)

This way you don't have to print to sys.stderr (which flushes by default).

The reason for your problem is line buffering. Line buffering makes I/O more efficient with the drawback of not immediately showing prints under some conditions.




ANSWER 3

Score 21


By default the level for logging is warning. So you won't see a logging message of level DEBUG. To fix this just enable debug logging with the basicConfig() function of the logging module:

import logging
logging.basicConfig(level=logging.DEBUG)



ANSWER 4

Score 13


Had the same printing problem. Using sys.stdout.flush() after the print solved the issue.