简体   繁体   中英

how to get python print result in jenkins console output

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?

在运行 python 脚本时尝试使用-u ( unbuffered ) 选项。

python -u my_script.py

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.

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

import sys
sys.stdout.flush()

It should help.

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 ie a=$(python_script) , so it was not being echoed....

To solve the problem I just had to log to stderr instead of stdout, ie

import sys
sys.stderr.write("my info\n")
sys.stdout.flush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM