While executing a bash subshell inside a Python script I had to inherit the environment variables, that can be done in this way:


$ cat ~/.bashrc |grep LOLO
export LOLO="/foo/bar"

*Here you need to restart your shell in order to execute .bashrc and export the variable.


echo $LOLO



import subprocess, os
my_env = os.environ.copy()
my_env["PATH"] = "/usr/sbin:/sbin:" + my_env["PATH"]
subprocess.Popen('/home/m.ortiz.montealegre/lolo/script.sh', env=my_env)


$ python script.py

The thing is, if you add new values (aliases / variables) to your bashrc you’ll need to restart your terminal in order to bashrc to get executed and make those changes availables in the environment.

From a subshell process you can’t set environment variables to the parent shell.