Python 2.7 changing default browser for webbrower.open

user3681278 picture user3681278 · Jun 4, 2014 · Viewed 7.1k times · Source

I am trying to open a page a wrote and saved to a local server. Everything is great but it defaults to opening in IE instead of Chrome. Chrome is my default browser and couldn't find any helpful tips online.

Sample code:

import webbrowser
webbrowser.open('192.168.1.254:1337/SmartFormTest1.php')

Thanks in advance!

Answer

user3681278 picture user3681278 · Jun 4, 2014

Alright, found the issue. My browser was correctly defaulted to chrome, the issue is the webbrowser.py file. Lines 539-563 read:

if sys.platform[:3] == "win":
class WindowsDefault(BaseBrowser):
    def open(self, url, new=0, autoraise=True):
        try:
            os.startfile(url)
        except WindowsError:
            # [Error 22] No application is associated with the specified
            # file for this operation: '<URL>'
            return False
        else:
            return True

_tryorder = []
_browsers = {}

# First try to use the default Windows browser
register("windows-default", WindowsDefault)

# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                        "Internet Explorer\\IEXPLORE.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
                "netscape", "opera", iexplore):
    if _iscommand(browser):
        register(browser, None, BackgroundBrowser(browse()

All I needed to do was add "chrome" to the list of for browser in (list).