Using subprocess to Run Python
00:00
In this lesson, you’ll be using subprocess
to run the timer Python app from within Python. So you’ll be running it from within the REPL. So the first thing to do is to import subprocess
,
00:17
and then from subprocess
,
00:20
as per the documentation, you use run()
. But then you need to open a list of arguments. So the first words that we used in the terminal as well was python
, and the second word was simple
00:35
_timer.py
. You then close the list and then close the brackets, and then hit Enter. And there you go. The timer starts, it says starting timer of five seconds.
00:48
You have your five dots, it prints Done
. So that’s as expected. And then it prints another line that says CompletedProcess
, and some things between brackets.
01:00
Now what this CompletedProcess
is, we will talk about that in the next video, but for now, congratulations. Because in your first example, you have launched from within Python, within the REPL, you have launched another Python process using subprocess
, and that subprocess then runs the simple_timer.py
app.
01:22 Now, you might wonder why we would do that. Why would you run another Python module from within a Python module? You could just import a Python module if you needed it.
01:33 There’s usually no need for another Python module to be in a separate process, and you would be right in thinking that. Now, the reason you will be using Python as a subprocess in your examples is that Python works on all platforms.
01:48
Although as we saw for some Linux and Mac users, it needs to be Python 3. subprocess
is usually used though for launching processes other than Python.
Become a Member to join the conversation.