Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling the "source" command from subprocess.Popen

Tags:

python

unix

popen

I have a .sh script that I call with source the_script.sh. Calling this regularly is fine. However, I am trying to call it from my python script, through subprocess.Popen.

Calling it from Popen, I am getting the following errors in the following two scenario calls:

foo = subprocess.Popen("source the_script.sh") Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/subprocess.py", line 672, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1213, in _execute_child     raise child_exception OSError: [Errno 2] No such file or directory   >>> foo = subprocess.Popen("source the_script.sh", shell = True) >>> /bin/sh: source: not found 

What gives? Why can't I call "source" from Popen, when I can outside of python?

like image 497
coffee Avatar asked Aug 12 '11 13:08

coffee


2 Answers

source is not an executable command, it's a shell builtin.

The most usual case for using source is to run a shell script that changes the environment and to retain that environment in the current shell. That's exactly how virtualenv works to modify the default python environment.

Creating a sub-process and using source in the subprocess probably won't do anything useful, it won't modify the environment of the parent process, none of the side-effects of using the sourced script will take place.

Python has an analogous command, execfile, which runs the specified file using the current python global namespace (or another, if you supply one), that you could use in a similar way as the bash command source.

like image 172
SingleNegationElimination Avatar answered Sep 21 '22 22:09

SingleNegationElimination


You could just run the command in a subshell and use the results to update the current environment.

def shell_source(script):     """Sometime you want to emulate the action of "source" in bash,     settings some environment variables. Here is a way to do it."""     import subprocess, os     pipe = subprocess.Popen(". %s; env" % script, stdout=subprocess.PIPE, shell=True)     output = pipe.communicate()[0]     env = dict((line.split("=", 1) for line in output.splitlines()))     os.environ.update(env) 
like image 43
xApple Avatar answered Sep 23 '22 22:09

xApple