Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sourcing output of python script fails with broken pipe exception

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

like image 516
Bryant Avatar asked Nov 08 '22 12:11

Bryant


1 Answers

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".

like image 79
l'L'l Avatar answered Nov 14 '22 22:11

l'L'l