Calling the "source" command from subprocess.Popen

popen, python, unix

Solution

`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`.

Problem

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?

Original source

Related problems