5

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
1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

xonsh shell should be useful for such mixing of bash and python

xonsh is a Python-powered shell that combines the flexibility of Python with > the convenience of a shell environment. It supports interactive use, > scripting, and seamless integration with existing Python libraries and tools.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.