subprocess with shell=True
specifically uses /bin/sh
. If this is not a symlink to bash, then you may be using a POSIX compliant shell (such as dash), then the source
command is not available:
$ cat > foo.sh
echo hello world
$ bash -c 'source ./foo.sh'
hello world
$ /bin/sh -c 'source ./foo.sh'
/bin/sh: 1: source: not found
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 7 2020 /bin/sh -> dash
$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('source ./foo.sh', shell=True)
/bin/sh: 1: source: not found
127
Since you explicitly made your script executable, use shell=False
Or, use the POSIX sh .
command instead of the bash-specific source
.
>>> subprocess.call('. ./foo.sh', shell=True)
hello world
0