
The recommended approach to invoking subprocesses is to use the run()įunction for all use cases it can handle. WebAssembly platforms for more information. It tells you how to accomplish sub-commands, get exit codes, piping, sub-commands and more.This module does not work or is not available on WebAssembly platforms The project’s documentation has lots more information and additional examples that are worth your time perusing. You need to pass a file or a file-like object to these arguments to make them work properly. You can also redirect stdout and stderr via some special keyword arguments: _out and _err. If you want to run a long-running process, the sh project supports putting it in the background via _bg=True argument. In this case, “-c 4” becomes “c=4”, which tells the ping command to only ping 4 times. Instead, you make them into Python-style arguments. You will notice that if the command accepts command-line arguments, you don’t include them in the string that you pass to the command. Next we imported the sh module and used it to open the Firefox browser to a specific web page. In this case, we imported the ls command and ran it against my home folder. First of all, you can just import the command name from the sh package. The examples above demonstrate several different concepts. ping statistics -Ĥ packets transmitted, 4 received, 0% packet loss, time 3004ms Let’s try it out with a few simple examples in Python’s interpreter:ĭesktop err.log nohup.out Pictures Templates To use sh, you just have to import the commands you want to use. Now that you have it installed, we’re ready to start learning!

To get started using the sh package, you will need to install it.

If you need Windows support, then you should try the pbs project. Note that at the time of writing, the sh package only supports Linux and Mac.

This article will go over a few examples to show you how to use this fun little library. Basically sh allows you to import and use shell commands directly from Python. These packages are wrappers for Python’s subprocess module. The other day, I came across an article about a fork of the pbs package called sh.
