Title: The subprocess Module: Wrapping Programs With Python – Real Python
Open Graph Title: The subprocess Module: Wrapping Programs With Python – Real Python
Description: In this tutorial, you'll learn how to leverage other apps and programs that aren't Python, wrapping them or launching them from your Python scripts using the subprocess module. You'll learn about processes all the way up to interacting with a process as it executes.
Open Graph Description: In this tutorial, you'll learn how to leverage other apps and programs that aren't Python, wrapping them or launching them from your Python scripts using the subprocess module. You'll learn about processes all the way up to interacting with a process as it executes.
Mail addresses
?subject=Python article for you&body=The subprocess Module: Wrapping Programs With Python on Real Python
https://realpython.com/python-subprocess/
Opengraph URL: https://realpython.com/python-subprocess/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "The subprocess Module: Wrapping Programs With Python",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/Subprocess_Watermarked.6d0e6a5d7d77.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/python-subprocess/",
"lastReviewed": "2025-01-18",
"author": {
"@type": "Person",
"name": "Ian Currie",
"image": "https://realpython.com/cdn-cgi/image/width=480,height=480,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/5sK42VEQ_-_Copy_-_Copy.b8a1a4a9ca1f.jpeg",
"url": "https://realpython.com/team/icurrie/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Aldren Santos",
"image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
"url": "https://realpython.com/team/asantos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Brenda Weleschuk",
"image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
"url": "https://realpython.com/team/bweleschuk/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Bartosz Zaczy\u0144ski",
"image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
"url": "https://realpython.com/team/bzaczynski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Geir Arne Hjelle",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
"url": "https://realpython.com/team/gahjelle/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Jim Anderson",
"image": "https://realpython.com/cdn-cgi/image/width=700,height=700,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jima.0b8f990b951a.jpg",
"url": "https://realpython.com/team/janderson/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Kate Finegan",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg",
"url": "https://realpython.com/team/kfinegan/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2025-01-18T14:00:00+00:00",
"dateModified": "2025-01-18T14:09:23.391741+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Ian Currie",
"image": "https://realpython.com/cdn-cgi/image/width=480,height=480,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/5sK42VEQ_-_Copy_-_Copy.b8a1a4a9ca1f.jpeg",
"url": "https://realpython.com/team/icurrie/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "In this tutorial, you'll learn how to leverage other apps and programs that aren't Python, wrapping them or launching them from your Python scripts using the subprocess module. You'll learn about processes all the way up to interacting with a process as it executes.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What\u2019s the Python subprocess module used for?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You use the Python subprocess module to launch and interact with child processes from within your Python programs. This allows you to run shell commands, GUI applications, or other programs as separate processes.
"
}
},
{
"@type": "Question",
"name": "How do you run a shell command using subprocess in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You run a shell command by using subprocess.run() and passing the command as a list of strings, or by setting shell=True if you need to run a shell script or command string directly.
"
}
},
{
"@type": "Question",
"name": "Can you pass input to a subprocess in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, you can pass input to a subprocess by using the input parameter in subprocess.run(), which allows you to send data to the process’s standard input.
"
}
},
{
"@type": "Question",
"name": "How do you capture the output of a subprocess?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You capture the output of a subprocess by using the capture_output=True parameter in subprocess.run(), which stores the standard output and error in the stdout and stderr attributes of the returned CompletedProcess object.
"
}
},
{
"@type": "Question",
"name": "What\u2019s the difference between .call(), .run(), and .Popen() in subprocess?",
"acceptedAnswer": {
"@type": "Answer",
"text": "subprocess.call() runs a command and waits for it to complete, returning the command’s exit status code. subprocess.run() is a more versatile function that returns a CompletedProcess object, allowing for greater control over input, output, and error handling. subprocess.Popen() provides fine-grained control over process creation and communication. It runs a process asynchronously so you can interact with it while it runs.
"
}
}
]
}
}
| author | Real Python |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/Subprocess_Watermarked.6d0e6a5d7d77.jpg |
| og:image | https://files.realpython.com/media/Subprocess_Watermarked.6d0e6a5d7d77.jpg |
| twitter:creator | @realpython |
| og:type | article |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large