'Call python script as module with input from bash script

From a bash function, I want to call a python script which prompts for input, and I need to run that script as a module using python -m

Here is select_pod.py

# above this will be a print out of pods
pod = input('Pick pod')
print(pod)

Here is the bash function:

function foo() {
  POD=$(python3 -m select_pod)
  kubectl exec $POD --stdin --tty bash
}

I can't get the input to work, i.e. "Pick pod" is not printed to the terminal.



Solution 1:[1]

You can change sys.stdout before input :

import sys
save_sys_stdout = sys.stdout
sys.stdout = sys.stderr
pod = input('Pick pod')
sys.stdout = save_sys_stdout
print(pod)

So that POD=$(python3 -m select_pod) will work and you don't need to do split after.

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 Philippe