How to source the bash file in python script and use the environment variable (which is set in bash script) inside python? When I am removing source file from python script and sourcing it outside the ROOT_DIR which is an environment variable in bash script is able to recognise by my python script. Could you please suggest how can I achieve sourcing the bash script and use the variables there in python script. An example I have given below.
#!/usr/bin/python3
import sys, getopt
import os
def main(argv):
os.system('source ./init.sh')
root_dir = os.environ['ROOT_DIR']
if __name__ == "__main__":
main(sys.argv[1:])
The error I got is below:
Sourcing Successfully Done... Traceback (most recent call last):
File "test.py", line 213, in main(sys.argv[1:]) File "test.py", line 33, in main root_dir = os.environ['ROOT_DIR'] File "/usr/lib64/python3.6/os.py", line 669, in getitem raise KeyError(key) from None KeyError: 'ROOT_DIR'
My bash script is as below:
export ROOT_DIR=${PWD}
export TB_DIR=${PWD}/..