I'm trying to have the output of a python script be sourceable. i.e. I'd like to be able to run:
$ source <(python example.py)
It ALWAYS fails with the same issue:
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
Here is example.py:
print("export ENV_VAR=abc")
Is there any way around this? I've attempted to try and catch the exception (BrokenPipeError) but it doesn't seem to work. The exception seems to prevent the sourcing from working since
$ echo $ENV_VAR
gives me nothing
Maybe eval
or export
could be used to get the variables from a Python
script into the current Bash
environment:
export $( python example.py )
echo $ENV_VAR
...or...
eval $( python example.py )
echo $ENV_VAR
There might be a better way to handle this, although both should output "abc".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With