I am running a python script
from the bash script
, and this python script
is responsible for fetching the environment variables and setting them up. My code is as follows:
Bash Script:
activate.sh
$(echo /path/to/my-script.py --my-profiles setenv)
Python Script:
my-script.py
def main():
parser = argparse.ArgumentParser(description='My script')
parser.add_argument('--my-profiles', dest="profiles",
type=str,
default='')
parsed_args = parser.parse_args()
if args.profiles == "setenv":
env = {'SOME_ENV': '1'}
for key, value in env.items():
os.environ[key] = value
When I ran the bash script, I expected that the SOME_ENV
would be set to 1
in the terminal where I am running the bash script. However, the environment variable is not set.
From little research, I think (maybe) this is because the python script creates a child process, and the environment variable is set in that child process and is not persistent in the terminal I am running my bash.
Is my understanding correct, if so, how to better deal with this situation?
NOTE: I get the environment variables as a dictionary from other tools. I only kept the part of the code that is necessary for this answer.