12

I have a Python script print strings. Now when run it in Jenkins I didn't see the printed strings in Jenkins Builds' Console Output.

Anyway to achieve that?

Grace
  • 173
  • 1
  • 1
  • 8

4 Answers4

19

Try using -u (unbuffered) option when running the python script.

python -u my_script.py

gh0st
  • 402
  • 5
  • 13
5

Any output to stdout from a process spawned by Jenkins should be captured by Console Output. One caveat is that it won't be displayed until a newline character is printed, so make sure your lines are terminated.

If you are launching python in some weird way that dis-associates it from Jenkins parent process, then I can't help you.

Slav
  • 27,057
  • 11
  • 80
  • 104
  • Thanks. It solves my problem. I used `sys.stdout.write('\r foo bar')` and `sys.stdout.flush()` to print the logs of a loop on the same line. It works as expected in my console. But in jenkins console output, the logs don't show until all are done. Now I test the value of the environmental variable `BUILD_NUMBER` to use `sys.stdout.write` or just `print`. – ElpieKay Jan 25 '22 at 14:05
2

I believe that what you need to do is a flush, try:

import sys
sys.stdout.flush()

It should help.

Suryavanshi
  • 595
  • 1
  • 7
  • 24
0

The accepted answer got me on the right track, however in my case, a shell script that Jenkins was running was capturing the output of the python script in a subshell to a variable i.e. a=$(python_script), so it was not being echoed....

To solve the problem I just had to log to stderr instead of stdout, i.e.

import sys
sys.stderr.write("my info\n")
sys.stdout.flush()
tjb
  • 11,480
  • 9
  • 70
  • 91