python's webbrowser launches IE, instead of default browser, on Windows relative path
My main issue was a bad URL by attempting prepend file://
to a relative path. It can be fixed with this:
webbrowser.open('file://' + os.path.realpath(filename))
Using webbrowser.open
will try multiple methods until one "succeeds", which is a loose definition.
The WindowsDefault
class calls os.startfile()
which fails and returns False
. I can verify that by entering the URL in the windows run command and seeing an error message rather than a browser.
Both GenericBrowser
and BackgroundBrowser
will call subprocess.Popen()
with an exe which will succeed, even with a bad URL, and return True
. IE gives no indication of the issue, all other browsers have a nice messages saying they can't find the file.
-
GenericBrowser
is set by the environment variableBROWSER
and is first. -
WindowsDefault
is second. -
BackgroundBrowser
is last and includes the fall back IE if nothing else works.
Here is my original setup:
>>> import webbrowser
>>> webbrowser._tryorder
['windows-default',
'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE']
>>> webbrowser._browsers.items()
[('windows-default', [<class 'webbrowser.WindowsDefault'>, None]),
('c:\\program files\\internet explorer\\iexplore.exe', [None, <webbrowser.BackgroundBrowser object at 0x00000000022E3898>])]
>>>
Here is my setup after modifiying the environment variables:
C:>path=C:\Program Files (x86)\Mozilla Firefox;%path%
C:>set BROWSER=C:\Users\Scott\AppData\Local\Google\Chrome\Application\chrome.exe
C:>python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import webbrowser
>>> webbrowser._tryorder
['C:\\Users\\Scott\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe',
'windows-default',
'firefox',
'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE']
>>> webbrowser._browsers.items()
[('windows-default', [<class 'webbrowser.WindowsDefault'>, None]),
('c:\\program files\\internet explorer\\iexplore.exe',[None, <webbrowser.BackgroundBrowser object at 0x000000000235E828>]),
('firefox', [None, <webbrowser.BackgroundBrowser object at 0x000000000235E780>]),
('c:\\users\\scott\\appdata\\local\\google\\chrome\\application\\chrome.exe', [None, <webbrowser.GenericBrowser object at 0x000000000235E8D0>])]
>>>
The webbrowser._tryorder
gives the list of browsers tried. Registering chrome or adding a BROWSER env var or modifiying my path all would have gotten me the correct browser with a better error message.
Thanks for the help guys, I couldn't have solved this without your ideas.
You can use get(name)
to use a specific browser.
You'll need to register the Chrome webbrowser, as it doesn't seem to be one of the predefined browser types, and then you should be able to do this:
webbrowser.get('chrome').open('http://www.google.com')
Update:
Actually, you might be able to just one of the following:
webbrowser.get('windows-default').open('http://www.google.com')
webbrowser.get('macosx').open('http://www.google.com')
The docs show no predefined defaults for Linux.
This opened a new Chrome tab for me, and it's still OS-independent:
webbrowser.get().open('http://www.google.com')
What's odd is that without the get()
call, it still uses IE. This looks like a bug with a simple workaround.
The webbrowser
module is supposed to use the default browser, so this might be a bug. On the other hand, use this explanation from the docs to troubleshoot your problem:
If the environment variable BROWSER exists, it is interpreted to override the platform default list of browsers, as a os.pathsep-separated list of browsers to try in order. When the value of a list part contains the string %s, then it is interpreted as a literal browser command line to be used with the argument URL substituted for %s; if the part does not contain %s, it is simply interpreted as the name of the browser to launch.
Using Windows 10, in short, everything that does not include a full URL in the https://example.com
format is opened in IE for me. For example, if I say
webbrowser.open("https://www.example.com")
it will open a new tab in Chrome, while
webbrowser.open("example.com")
will open IE. Any .get()
will cause it to not open a browser at all.
Kind of weird behaviour, but I can see that this is a complex thing do implement and likely the OS is to blame for this behavior.