简体   繁体   中英

Replicate shell environment in python for executing bash commands

I'm trying to execute a bash script from inside python using the subprocess module. The problem is that this bash script relies on many environment variables that are set in my .bashrc file. What I want is for python subprocess to exactly replicate the environment that is setup when I launch a terminal window. So far I tried:

p = subprocess.Popen(args=['my-script.sh', '--additional_args'], stdout=subprocess.PIPE, env=os.environ.copy(), shell=True)
output_buffer, return_code = p.communicate()

But then read somewhere that even after doing shell=True , the .bashrc file is still not loaded, and that i should try something like this:

p = subprocess.Popen(args=['/bin/bash', '-i', '-c', 'my_script.sh', '--additional_args'], stdout=subprocess.PIPE)
output_buffer, return_code = p.communicate()

But with this function call, I get this error:

tput: No value for $TERM and no -T specified
bash: cannot set terminal process group (2071): Inappropriate ioctl for device
bash: no job control in this shell

This worked for me:

Bashrc

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

Bash

#!/usr/bin/bash

echo $LOLO

Python

#!/usr/bin/python

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)

Output:

$ python script.py
/foo/bar

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

I've found the references to do this here and here

Note: From a subshell process you can't set environment variables to the parent shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM