Python Call Shell Command

ifeelfree
1 min readMar 13, 2021

There are several ways of calling shell command in Python scripts, and very often I found that when calling shell commands, it will not finish running before it begin to run Python codes next to the shell command.

After many tries, the following codes can call shell command successfully sometimes (but not all the time):

def execute_command(cmd):
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
stdout, stderr = process.communicate()

process.wait()

return_code = process.returncode

if return_code < 0:
raise Exception("fail to run "+str(cmd))

An example of calling this command is:

print("install ffmpeg")
execute_command('apt update')
execute_command('apt install ffmpeg')
time.sleep(90)

Here we also use time.sleep to make sure that the command can finish before running next lines.

Problem 1

It may happen sometimes you will receive the following error messages:

Error: mkl-service + Intel(R) MKL: MKL_THREADING_LAYER=INTEL is incompatible with libgomp.so.1 library.

The solution is to use

os.environ['MKL_THREADING_LAYER'] = 'GNU'

Reference

--

--