'Python print output not appearing when called from bash

Here is a minimal working example:

I have a python script test.py that contains:

print("Hello")

and I have a bash script test.sh that calls that python function

#!/usr/bin/bash
python test.py

and when I run test.sh from terminal there is no output.

Based on a few similar questions, I have tried appending sys.stdout.flush and calling python -u instead, but there is still no output.

How do I get the output of print to show up?

Edit

In more complicated examples, how do I ensure that python print statements appear when called within a bash script? And ensure that those statements can be appropriately redirected with, e.g. &> operators?

(Also, I tried searching for a while before asking, but couldn't find a question that addressed this exactly. Any links to more thorough explanations would be greatly appreciated!)



Solution 1:[1]

My python output was missing when assigning it to a bash variable. I can't replicate your exact issue either, but I think this could help:

#!/usr/bin/bash

script_return=$(python test.py)
echo "$script_return"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ArturoE