91

What can I use to call the OS to open a URL in whatever browser the user has as default? Not worried about cross-OS compatibility; if it works in linux thats enough for me!

Jonas
  • 121,568
  • 97
  • 310
  • 388
Bolster
  • 7,460
  • 13
  • 61
  • 96
  • What os are you looking for since Windows, MacOS and Linux all have a different calling convention. – koblas Nov 18 '10 at 16:18

5 Answers5

152

Here is how to open the user's default browser with a given url:

import webbrowser

url = "https://www.google.com/"

webbrowser.open(url, new=0, autoraise=True)

Here is the documentation about this functionality. It's part of Python's stdlibs:

http://docs.python.org/library/webbrowser.html

I have tested this successfully on Linux, Ubuntu 10.10.

kobrien
  • 2,931
  • 2
  • 24
  • 33
  • 2
    On OS X 10.8.2 with Python 2.7.2 this does *not* appear to work. However, it works fine on Windows 7 with Python 2.7.3. Also works fine on Ubuntu 12.04 with XFCE 4.8 and Python 2.7.3. – leetNightshade Apr 25 '13 at 16:14
  • Okay, on OS X 10.8.2 with Python 2.7.4 this does appear to work. So, annoyingly it probably only reliably works on newer versions of Python. – leetNightshade May 02 '13 at 20:35
  • 4
    I get webbrowser.open(url[, new=0[, autoraise=True]]) ^ SyntaxError: invalid syntax – fatuhoku Aug 28 '13 at 10:02
  • I'm using OSX 10.8.4 with Python 2.7.2 and verified it does indeed work. However, if you use the url 'google.com' it fails without error. You need to specify 'http://google.com'. – Josh Dec 18 '13 at 01:27
  • 2
    FWIW, this is what `import antigravity` uses: https://hg.python.org/cpython/file/tip/Lib/antigravity.py – Sumit Feb 17 '16 at 07:35
  • I'm using Windows 7 64bit with Python 2.7.10 and it always opened a browser. But sometimes that browser was Internet Explorer instead of the default (Firefox) browser. – ChaimG Feb 19 '17 at 01:58
45

Personally I really wouldn't use the webbrowser module.

It's a complicated mess of sniffing for particular browsers, which will won't find the user's default browser if they have more than one installed, and won't find a browser if it doesn't know the name of it (eg Chrome).

Better on Windows is simply to use the os.startfile function, which also works on a URL. On OS X, you can use the open system command. On Linux there's xdg-open, a freedesktop.org standard command supported by GNOME, KDE and XFCE.

if sys.platform=='win32':
    os.startfile(url)
elif sys.platform=='darwin':
    subprocess.Popen(['open', url])
else:
    try:
        subprocess.Popen(['xdg-open', url])
    except OSError:
        print 'Please open a browser on: '+url

This will give a better user experience on mainstream platforms. You could fall back to webbrowser on other platforms, perhaps. Though most likely if you're on an obscure/unusual/embedded OS where none of the above work, chances are webbrowser will fail too.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 7
    I've just looked at the source for webbrowser, and I'm not sure I agree with you. Only the unix sniffing looks a bit unreliable, and even it should work correctly in KDE or GNOME (it probably could use a patch to use `xdg-open`, though `xdg-open` uses similar sniffing anyway). The win32 implementation, for instance, uses `os.startfile()` already, and it also has a fallback. – Liquid_Fire Nov 18 '10 at 23:52
  • 1
    The webbrowser module worked for me when I had Safari as my default browser, and also when I had Chrome as my default browser on Mac. – daviewales Feb 04 '13 at 15:31
  • 13
    Note that webbrowser uses `xdg-open` now, too. Thus this answer is outdated on modern python and there is no reason not to use the webbrowser module. – Chronial Sep 19 '17 at 14:25
8

You can use the webbrowser module.

webbrowser.open(url)
Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
7

Then how about mixing codes of @kobrien and @bobince up:

import subprocess
import webbrowser
import sys

url = 'http://test.com'
if sys.platform == 'darwin':    # in case of OS X
    subprocess.Popen(['open', url])
else:
    webbrowser.open_new_tab(url)
Community
  • 1
  • 1
Kenial
  • 2,510
  • 23
  • 26
2

Have a look at the webbrowser module.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820