Is there a way to determine which print statement produces a line of output in a python terminal? The use case is a large body of code across multiple files wherein it would be difficult or time-consuming to track down the specific line resulting in a printed statement. As a result, during runtime, I can't tell where the print statements are being executed from.
Asked
Active
Viewed 802 times
-1
-
use `grep` in a linux or `findstr` https://mkyong.com/linux/grep-for-windows-findstr-example/ – azro Oct 09 '20 at 20:26
-
Use a debugger? – OneCricketeer Oct 09 '20 at 20:28
-
Can't use grep or findstr because the output is a variable which is converted to a string rather than a string literal, so you'd need to know the variable name rather than the variable value (which is printed) to search it – DerekG Oct 09 '20 at 20:42
2 Answers
3
My recommendation would be import logging
and use that rather than using print
Then configure it to include line numbers and module names

OneCricketeer
- 179,855
- 19
- 132
- 245
0
A simple way to tell would be to just add some indicators, whether they be line numbers or the context surrounding the print statement:
print('put info here and this statement was executed from line x in file x')
...

Kevin Sheng
- 411
- 1
- 5
- 8
-
2Sounds like the OP doesn't want to edit the file. Otherwise, they would _already know_ where the print happened – OneCricketeer Oct 09 '20 at 20:29