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
43
votes
1 answer

Execute terminal commands in python3

I am on a Raspberry Pi, and I am using a program called fswebcam, which allows you to take pictures with a webcam. ~$ fswebcam image.jpg That command if entered in terminal takes a picture and saves it to your computer, however I want to build a…
BrandonMayU
  • 726
  • 2
  • 6
  • 13
43
votes
3 answers

How to avoid [Errno 12] Cannot allocate memory errors caused by using subprocess module

Complete Working Test Case Of course depending on your memory on the local and remote machines your array sizes will be different. z1 = numpy.random.rand(300000000,2); for i in range(1000): print('*******************************************\n');…
Paul
  • 7,155
  • 8
  • 41
  • 40
42
votes
4 answers

Run ffmpeg without outputting configuration information?

I'm invoking ffmpeg with subprocess.Popen, and trying to capture the stderr output and write it to logging. args = ['ffmpeg', '-i', path] if start: args += ['-ss', start] if end: args += ['-t', end] args += [ '-vcodec', 'copy', …
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
42
votes
4 answers

How do I run another script in Python without waiting for it to finish?

I am creating a little dashboard for a user that will allow him to run specific jobs. I am using Django so I want him to be able to click a link to start the job and then return the page back to him with a message that the job is running. The…
sheats
  • 33,062
  • 15
  • 45
  • 44
41
votes
2 answers

Getting an error - AttributeError: 'module' object has no attribute 'run' while running subprocess.run(["ls", "-l"])

I am running on a AIX 6.1 and using Python 2.7. Want to execute following line but getting an error. subprocess.run(["ls", "-l"]) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no…
nisarga lolage
  • 531
  • 1
  • 5
  • 14
41
votes
3 answers

subprocess.Popen in different console

I hope this is not a duplicate. I'm trying to use subprocess.Popen() to open a script in a separate console. I've tried setting the shell=True parameter but that didn't do the trick. I use a 32 bit Python 2.7 on a 64 bit Windows 7.
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
41
votes
1 answer

Python subprocess and user interaction

I'm working on a GUI front end in Python 2.6 and usually it's fairly simple: you use subprocess.call() or subprocess.Popen() to issue the command and wait for it to finish or react to an error. What do you do if you have a program that stops and…
Dave Brunker
  • 1,559
  • 5
  • 15
  • 23
41
votes
4 answers

How to get output from subprocess.Popen(). proc.stdout.readline() blocks, no data prints out

I want output from execute Test_Pipe.py, I tried following code on Linux but it did not work. Test_Pipe.py import time while True : print "Someting ..." time.sleep(.1) Caller.py import subprocess as subp import time proc =…
wearetherock
  • 3,721
  • 8
  • 36
  • 47
41
votes
7 answers

Using subprocess.Popen for Process with Large Output

I have some Python code that executes an external app which works fine when the app has a small amount of output, but hangs when there is a lot. My code looks like: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,…
Tim
  • 6,851
  • 11
  • 42
  • 46
40
votes
4 answers

How to just call a command and not get its output

In Python, what is the shortest and the standard way of calling a command through subprocess but not bothering with its output. I tried subprocess.call however it seems to return the output. I am not bothered with that, I just need to run the…
user225312
  • 126,773
  • 69
  • 172
  • 181
40
votes
2 answers

How do i use subprocesses to force python to release memory?

I was reading up on Python Memory Management and would like to reduce the memory footprint of my application. It was suggested that subprocesses would go a long way in mitigating the problem; but i'm having trouble conceptualizing what needs to be…
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
39
votes
6 answers

Python os.system without the output

I'm running this: os.system("/etc/init.d/apache2 restart") It restarts the webserver, as it should, and like it would if I had run the command directly from the terminal, it outputs this: * Restarting web server apache2 …
user697108
  • 3,521
  • 3
  • 20
  • 14
39
votes
4 answers

How do you list all child processes in python?

I'm using a third party library that starts various sub processes. When there's an exception I'd like to kill all the child processes. How can I get a list of child pids?
Rowan
  • 874
  • 1
  • 6
  • 14
38
votes
6 answers

Python OSError: [Errno 2]

I have the following code that is attempting to start each of the "commands" below in Linux. The module attempts to keep each of the 2 commands running if either should crash for whatever reason. #!/usr/bin/env python import subprocess commands = […
Caedis
  • 591
  • 1
  • 8
  • 13
38
votes
1 answer

Suppress stderr within subprocess.check_output()

I'm trying to find a way to ignore the stderr stream (something similar to 2> /dev/null): output = subprocess.check_output("netstat -nptl".split()) What should I add to the above command to achieve this?
triple fault
  • 13,410
  • 8
  • 32
  • 45