Learning the subprocess Module
00:00
In this lesson, you’ll go through a quick overview of the subprocess
module. Why is it called subprocess
? Well, that module is used for launching processes from within Python.
00:14 So these launched processes are called child processes and Python, therefore, is the parent process that spawns, I love that word, it spawns or it launches a child process.
00:27 Now that process could be anything. It could be a GUI application, it could be the shell, or as you will see in your first example in the next video, it could even be Python itself.
00:38
Now, the subprocess
module was originally proposed and accepted for Python 2.4 as an alternative to the os
module. The examples in this course were tested with Python 3.10.12, but all you need if you want to code along is Python 3.8 or later.
00:59
The documentation recommends using run()
for all cases it can handle. And for more special cases where run()
doesn’t do what you want it to do, you can use the Popen
class.
01:10
The Popen
class is the underlying class for the whole subprocess
module. And what that means is that all the functions in the subprocess
module are in fact convenience wrappers around the Popen()
constructor and its instance methods.
01:29
And if that sounds confusing to you, it surely does to me, but worry not, in a later video in this course, you will learn all about the Popen
class.
01:39
Now, during your research online, you might come across other functions than run()
. You might come across call()
or check_call()
or check_output()
, but these actually belong to the older subprocess
API for Python 3.5 and earlier.
01:56
But everything that these functions do, you can do with run()
as well.
02:02
The subprocess
module was proposed as an alternative to the os
module. Now os
stands for operating system and the os
module deals with operating system dependent functionality.
02:16
Now that is pretty low-level stuff and therefore, the subprocess
module is also quite a low-level utility. That means you might need to take account of which operating system you’re working on, and you might have to deal with some other tricky details.
02:33
Say you wanted Python to open a web browser. You could do that using subprocess
, but you would need to deal with cross-platform and browser differences.
02:43
A better choice potentially would be to just use the Python webbrowser
module, which, in fact, uses subprocess
under the hood, but it deals with those cross-platform and browser differences for you.
02:58
But having said that, subprocess
is really useful for getting something done quickly. And with that in mind, you will build a quick timer example in the next lesson.
Become a Member to join the conversation.