cmd = ['command', 'option1', 'option2', '...']
Run a command, block until it finishes, and get the return code
returncode = subprocess.call(cmd)
Run a command, block until it finishes, and get its output to stdout and stderr and the return code
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
returncode = proc.returncode
Run a command, do not block but read the output from the process, and get the return code when it finishes
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output_text = ''
output = proc.stdout.read(1)
if output == '': break
output_text += output
returncode = proc.wait()
Run a command, and send the input to the process
input_text = 'some input to the process'
proc = subprocess.Popen(cmd, stdin=PIPE)
If it fails to install matplotlib on mac because the compiler cannot locate some freetype header files, and if you don’t have freetype, then first install it using brew for instance:
brew install freetype
Odds are the compiler still complains when you try the installation second time.
The reason is that many libraries are installed under /usr/X11 on mac, and they are not correctly located at compile time due to the non-canonical location. If you’re using pkg-config, an easy way to use them is to include the package database there in /usr/X11 when locating headers and libraries using pkg-config. Export the following variable before compiling matplotlib:
pip install matplotlib
subprocess.Popen non-blocking pipe I/O using an additional thread and an asynchronous queue:
Why subprocess.Popen fails on Windows with a command having a Unicode filename within it: it internally uses CreateProcessA, which is the single-byte character version of CreateProcess, meaning that it won’t handle Unicode characters correctly. For this to be correctly handled, the command line, and hence the filename contained, needs to be encoded with the file system encoding, that is usually MBCS.
This thread and the pep below describe exactly this problem as well as the workaround above mentioned.
You will need to specify the encoding of the Python file if you use Unicode characters there: most simply add # encoding=utf-8 as the first or the second line of your source code although there are some more degrees of freedom, which can be found here.