args is required for all calls and should be a string, or a sequence ofprogram arguments. Providing a sequence of arguments is generallypreferred, as it allows the module to take care of any required escapingand quoting of arguments (e.g. to permit spaces in file names). If passinga single string, either shell must be True (see below) or elsethe string must simply name the program to be executed without specifyingany arguments.
The underlying process creation and management in this module is handled bythe Popen class. It offers a lot of flexibility so that developersare able to handle the less common cases not covered by the conveniencefunctions.
For maximum reliability, use a fully qualified path for the executable.To search for an unqualified name on PATH, useshutil.which(). On all platforms, passing sys.executableis the recommended way to launch the current Python interpreter again,and use the -m command-line format to launch an installed module.
If group is not None, the setregid() system call will be made in thechild process prior to the execution of the subprocess. If the providedvalue is a string, it will be looked up via grp.getgrnam() andthe value in gr_gid will be used. If the value is an integer, itwill be passed verbatim. (POSIX only)
If extra_groups is not None, the setgroups() system call will bemade in the child process prior to the execution of the subprocess.Strings provided in extra_groups will be looked up viagrp.getgrnam() and the values in gr_gid will be used.Integer values will be passed verbatim. (POSIX only)
If user is not None, the setreuid() system call will be made in thechild process prior to the execution of the subprocess. If the providedvalue is a string, it will be looked up via pwd.getpwnam() andthe value in pw_uid will be used. If the value is an integer, it willbe passed verbatim. (POSIX only)
Popen and the other functions in this module that use it raise anauditing event subprocess.Popen with argumentsexecutable, args, cwd, and env. The value for argsmay be a single string or a list of strings, depending on platform.
The most common exception raised is OSError. This occurs, for example,when trying to execute a non-existent file. Applications should prepare forOSError exceptions. Note that, when shell=True, OSErrorwill be raised by the child only if the selected shell itself was not found.To determine if the shell failed to find the requested application, it isnecessary to check the return code or output from the subprocess.
When the timeout parameter is not None, then (on POSIX) thefunction is implemented using a busy loop (non-blocking call and shortsleeps). Use the asyncio module for an asynchronous wait: seeasyncio.create_subprocess_exec.
This module also provides the following legacy functions from the 2.xcommands module. These operations implicitly invoke the system shell andnone of the guarantees described above regarding security and exceptionhandling consistency are valid for these functions.
Setting this has no impact on use of posix_spawn() which could usevfork() internally within its libc implementation. There is a similarsubprocess._USE_POSIX_SPAWN attribute if you need to prevent use ofthat.
The Python subprocess module is for launching child processes. These processes can be anything from GUI applications to the shell. The parent-child relationship of processes is where the sub in the subprocess name comes from. When you use subprocess, Python is the parent that creates a new child process. What that new child process is, is up to you.
Python subprocess was originally proposed and accepted for Python 2.4 as an alternative to using the os module. Some documented changes have happened as late as 3.8. The examples in this article were tested with Python 3.10.4, but you only need 3.8+ to follow along with this tutorial.
The subprocess module is mainly for calling programs other than Python. But, as you can see, you can call Python too if you want! For more discussion on the use cases of subprocess, check out the section where this is discussed in more depth, or one of the later examples.
Shells typically do their own tokenization, which is why you just write the commands as one long string on the command line. With the Python subprocess module, though, you have to break up the command into tokens manually. For instance, executable names, flags, and arguments will each be one token.
The split() function divides a typical command into the different tokens needed. The shlex module can come in handy when it may be not obvious how to divide up more complex commands that have special characters, like spaces:
Even though specific libraries might be able to do your task, it may still be worth doing things with subprocess. For one, it might be much faster for you to execute what you already know how to do, rather than learning a new library.
The run() function with the Shell parameter will almost always end up using the Command Prompt. The subprocess module uses the Windows COMSPEC environment variable, which in almost all cases will point to cmd.exe, the Command Prompt. By now, there are so many programs that equate COMSPEC to cmd.exe that changing it would cause much breakage in unexpected places! So, changing COMSPEC is generally not advised.
If at any point you plan to get user input and somehow translate that to a call to subprocess, then you have to be very careful of injection attacks. That is, take into account potential malicious actors. There are many ways to cause havoc if you just let people run code on your machine.
You can imagine the intended use case is to wrap ls and add something to it. So the expected user behavior is to provide a path like "/home/realpython/". However, if a malicious actor realized what was happening, they could execute almost any code they wanted. Take the following, for instance, but be careful with this:
You can think of the standard I/O streams as byte dispensers. The subprocess fills up stdout and stderr, and you fill up stdin. Then you read the bytes in stdout and stderr, and the subprocess reads from stdin.
Text mode means that subprocess will try to take care of encoding itself. To do that, it needs to know what character encoding to use. Most of the options for doing this in subprocess will try to use the default encoding. However, you generally want to be explicit about what encoding to use to prevent a bug that would be hard to find in the future.
There are other ways to put run() into text mode. You can also set a True value for errors or universal_newlines, which will also put run() into text mode. This may seem redundant, but much of this is kept for backwards compatibility, seeing as the subprocess module has changed over the years.
The input() function will read from stdin until it reaches a newline, which means an Enter keystroke in this context. It returns everything it consumed from stdin except the newline. With that knowledge, you can use subprocess to interact with this game:
Using subprocess is often tricky to get working across different platforms, and it has inherent dangers. But even though it may involve some sloppy Python, using subprocess can be a very quick and efficient way to solve a problem.
Could this be done with Cookiecutter? Could you use GitPython for the git part? Could you use the venv module to create the virtual environment? Yes to all. But if you just need something quick and dirty, using commands you already know, then just using subprocess can be a great option.
As mentioned, the underlying class for the whole subprocess module is the Popen class and the Popen() constructor. Each function in subprocess calls the Popen() constructor under the hood. Using the Popen() constructor gives you lots of control over the newly started subprocesses.
I want to use this data inside a Python script, so I need to be able to capture the output of the above command via the subprocess module. The following works, but just returns a huge string since I'm not restricting it to the top 3:
How do I use the pipe symbol inside Python, or use some other way to do the sorting without having to do incredible amounts of parsing on the huge string returned by psResult = subprocess.check_output(['ps', '-eo', 'pcpu,user,args'])?
I'm currently using Python 2.7 on a unix environment.I need to run R scripts in my python scripts but I can't manage to make it work because my R module needs to be loaded before (using "module load")
I am attempting to write a python script that will reproject & merge a series of .tif files then clip them to the boundaries of a .shp file. I have successfully used the subprocess module to do step 1, but am not sure how to use subprocess to run another second operation. Below is an example of what it might look like if I used subprocess/GDAL to reproject one set of files, then reproject a second set in another folder in the same script. Any ideas on why this won't work?
In your case I see some problems that may cause your script to "not work". First, you should specify the correct path to your programs and to your data. Working with relative paths is problematic, since your script may not work under the same shell environment as you do. So, your relative path newfolder/band4.tif may be the reason why the command does not work.Second, you are fiddling too much with python commands you obviously do not understand well enough. If you want to continue to use the subprocess.Popen it's best to read the manual page and understand it.
1) subprocess.check_call() waits for the completion of the command before it continues in your script. Nice for seeing where your problem lies and vital if you want to treat the same data sequentially. You may substitute subprocess.check_call() with other subprocess commands after your program runs
I am going to put my notes and experiences about this module here. Please note, I wrote this withPython 2.7 in mind. Things are slightly different in other versions (even 2.6). If you find anyerrors or suggestions, please let me know.
df19127ead