linux - Using shell variables in Python script is not working -
i have shell script parameters being set like:
k.sh:
export var="value" export val2="value2"
then have python script calling shell script , want use these enviornment variables
ex1.py:
import subprocess import os subprocess.call("source k.sh",shell=true) print os.environ["var"]
but getting keyerror
how can use these shell variable in python script?
you source k.sh
and run python one-liner print contents of os.environ
json. use json.loads
convert output dict in main process:
import subprocess import os import json pipe = subprocess.pipe output = subprocess.check_output( ". ~/tmp/k.sh && python -c 'import os, json; print(json.dumps(dict(os.environ)))'", shell=true) env = json.loads(output) print(env["var"])
yields
value
Comments
Post a Comment