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?
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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With