output = subprocess.check_output(command, shell=True, executable='/bin/bash')
os.popen() is implemented in terms of subprocess module.
I am dealing with a single environment in which I am interacting with many environment variables etc.
- each - os.popen(cmd)call creates a new- /bin/shprocess, to run- cmdshell command.
 - Perhaps, it is not obvious from the - os.popen()documentation that says:
 - 
- Open a pipe to or from command - cmd
 
 - "open a pipe" does not communicate clearly: "start a new shell process with a redirected standard input or output" -- your could report a documentation issue. - If there is any doubt; the source confirms that each successful - os.popen()call creates a new child process
 
- the child can't modify its parent process environment (normally). 
Consider:
import os
#XXX BROKEN: it won't work as you expect
print(os.popen("export VAR=value; echo ==$VAR==").read())
print(os.popen("echo ==$VAR==").read())
Output:
==value==
====
==== means that $VAR is empty in the second command because the second command runs in a different /bin/sh process from the first one.
To run several bash commands inside a single process, put them in a script or pass as a string:
output = check_output("\n".join(commands), shell=True, executable='/bin/bash')
Example:
#!/usr/bin/env python
from subprocess import check_output
output = check_output("""
    export VAR=value; echo ==$VAR==
    echo ==$VAR==
    """, shell=True, executable='/bin/bash')
print(output.decode())
Output:
==value==
==value==
Note: $VAR is not empty here.
If you need to generate new commands dynamically (based on the output from the previous commands); it creates several issues and some of the issues could be fixed using pexpect module: code example.