0

I'm writing an automation program and I'm using subprocess.call to open Google Chrome. The code for this is

from subprocess import call
...
call('google-chrome')

When I run this program through the terminal Chrome launches but it remains tied to the terminal. If I try call('google-chrome &') I get No such file or directory in my traceback. ($ google-chrome & when entered in terminal will launch Chrome but it won't tie it to the terminal).

I've also tried call(['google-chrome', '&']) but this opens Chrome still tied to the terminal but it thinks the & is an argument for a website I want to open.

Should I be using subprocess.call for this?

1 Answer 1

3

Don't use call(); it'll wait for the process to complete.

Use the subprocess.Popen() class directly, without waiting:

from subprocess import Popen

Popen(['google-chrome'])

You can redirect its output to the bit bin, if desired:

from subprocess import Popen, STDOUT
import os

Popen(['google-chrome'], stdout=os.open(os.devnull, os.O_RDWR), stderr=STDOUT)

If all you want do do is spawn a browser with a given URL, take a look at the webbrowser module; it uses the same call under the hood to spawn a browser process in the background (including the redirection to /dev/null). Although Google Chrome is not listed on the documentation page, the 2.7.5 source code of the module does list google-chrome and chrome as recognized browser strings.

Sign up to request clarification or add additional context in comments.

7 Comments

This seems to have worked but it's still printing tracebacks when I use Chrome as I normally would.
'still printing tracebacks'? Not sure what you mean.
I have a debug message to print after it launches Chrome and it prints. After that however there's a lot of information appearing in my terminal coming from Chrome. See this screenshot.
I've added an option to discard all output from Chrome, by redirecting its stdout and stderr to /dev/null.
@Nanor: I cannot reproduce your problem; with the /dev/null redirection I don't see output to the console anymore. Are you certain you are running the correct code and that no pre-existing Chrome browser was open?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.