Questions tagged [subprocess]

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Use it to run a shell command or an executable in Python.

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

If you want to run Python code in a separate process consider instead.

is preferable to subprocess in some cases.

Waiting for a command to finish and getting the result

Interacting with a subprocess while it is still running

Windows

Misc

12158 questions
58
votes
9 answers

running a command as a super user from a python script

So I'm trying to get a process to be run as a super user from within a python script using subprocess. In the ipython shell something like proc = subprocess.Popen('sudo apach2ctl restart', shell=True, stdin=subprocess.PIPE, …
Silfheed
  • 11,585
  • 11
  • 55
  • 67
58
votes
4 answers

Interactive input/output using Python

I have a program that interacts with the user (acts like a shell), and I want to run it using the Python subprocess module interactively. That means, I want the possibility to write to standard input and immediately get the output from standard…
Talor Abramovich
  • 801
  • 1
  • 6
  • 9
58
votes
10 answers

Launch IPython notebook with selected browser

I am trying to start IPython with a non default browser (in my case Firefox) and thought I could replicate the replicate the script given in this blog I am on Windows 7 I put the following code in a file say "module.py" import…
user1043144
  • 2,680
  • 5
  • 29
  • 45
58
votes
7 answers

Timeout on subprocess readline in Python

I have a small issue that I'm not quite sure how to solve. Here is a minimal example: What I have scan_process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while(some_criterium): line =…
Tom
  • 3,807
  • 4
  • 33
  • 58
57
votes
9 answers

gzip a file in Python

I want to gzip a file in Python. I am trying to use the subprocss.check_call(), but it keeps failing with the error 'OSError: [Errno 2] No such file or directory'. Is there a problem with what I am trying here? Is there a better way to gzip a file…
Rinks
  • 1,007
  • 2
  • 16
  • 22
57
votes
1 answer

Opening a process with Popen and getting the PID

I'm working on a nifty little function: def startProcess(name, path): """ Starts a process in the background and writes a PID file returns integer: pid """ # Check if the process is already running status, pid =…
Hubro
  • 56,214
  • 69
  • 228
  • 381
57
votes
2 answers

Python spawn off a child subprocess, detach, and exit

I'm wondering if this is the correct way to execute a system process and detach from parent, though allowing the parent to exit without creating a zombie and/or killing the child process. I'm currently using the subprocess module and doing…
Ilya Sterin
  • 697
  • 1
  • 5
  • 6
56
votes
6 answers

check output from CalledProcessError

I am using subprocess.check_output from pythons subprocess module to execute a ping command. Here is how I am doing it: output = subprocess.check_output(["ping","-c 2 -W 2","1.1.1.1") It is raising a CalledProcessError and says the output is one of…
ash
  • 781
  • 1
  • 9
  • 20
56
votes
1 answer

What is the use of stub files (.pyi ) in python?

I am trying to understand the lower level implementations of python 3. There is one module named _posixsubprocess used by the subprocess module. I tried to find the location of this module in my system and found that it's a stub file. Could someone…
Rahul Gusai
  • 701
  • 1
  • 6
  • 11
56
votes
3 answers

Piping output of subprocess.Popen to files

I need to launch a number of long-running processes with subprocess.Popen, and would like to have the stdout and stderr from each automatically piped to separate log files. Each process will run simultaneously for several minutes, and I want two log…
user280867
55
votes
8 answers

Using subprocess to run Python script on Windows

Is there a simple way to run a Python script on Windows/Linux/OS X? On the latter two, subprocess.Popen("/the/script.py") works, but on Windows I get the following error: Traceback (most recent call last): File "test_functional.py", line 91, in…
dbr
  • 165,801
  • 69
  • 278
  • 343
55
votes
4 answers

Run child processes as different user from a long running Python process

I've got a long running, daemonized Python process that uses subprocess to spawn new child processes when certain events occur. The long running process is started by a user with super user privileges. I need the child processes it spawns to run as…
Peter Parente
  • 1,269
  • 2
  • 12
  • 16
53
votes
5 answers

Python - how to execute shell commands with pipe, but without 'shell=True'?

I have a case to want to execute the following shell command in Python and get the output, echo This_is_a_testing | grep -c test I could use this python code to execute the above shell command in python, >>> import subprocess >>>…
user1129812
  • 2,859
  • 8
  • 32
  • 45
51
votes
7 answers

Merging a Python script's subprocess' stdout and stderr while keeping them distinguishable

I would like to direct a python script's subprocess' stdout and stdin into the same file. What I don't know is how to make the lines from the two sources distinguishable? (For example prefix the lines from stderr with an exclamation mark.) In my…
beemtee
  • 831
  • 1
  • 8
  • 10
51
votes
8 answers

subprocess: deleting child processes in Windows

On Windows, subprocess.Popen.terminate calls win32's TerminalProcess. However, the behavior I see is that child processes of the process I am trying to terminate are still running. Why is that? How do I ensure all child processes started by the…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187