I'm trying to register the Firefox browser to run on Windows. According to the documentation for Webbrowser, "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". I have the following:
import os
import webbrowser
from subprocess import call
os.environ["BROWSER"] = "C:\\FirefoxPortable\\FirefoxPortable.exe"
webbrowser.open('http://google.com')
This still opens iexplorer ( the default browser ).
Also:
>>> webbrowser._browsers
{'windows-default': [<class 'webbrowser.WindowsDefault'>, None], 'c:\\program files\\internet explorer\\iexplore.exe': [None, <webbrowser.BackgroundBrowser object at 0x04A18F90>]}
>>> webbrowser._tryorder
['windows-default', 'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE']
How Can I use Firefox here?
Source:
# OK, now that we know what the default preference orders for each
# platform are, allow user to override them with the BROWSER variable.
if "BROWSER" in os.environ:
_userchoices = os.environ["BROWSER"].split(os.pathsep)
_userchoices.reverse()
# Treat choices in same way as if passed into get() but do register
# and prepend to _tryorder
for cmdline in _userchoices:
if cmdline != '':
cmd = _synthesize(cmdline, -1)
if cmd[1] is None:
register(cmdline, None, GenericBrowser(cmdline), -1)
cmdline = None # to make del work if _userchoices was empty
del cmdline
del _userchoices
# what to do if _tryorder is now empty?
webbrowser
module is included with the Python distribution and is inC:\PythonX\Lib\webbrowser.py
, so you could make a copy of it for debugging purposes to find the problem. Look for the lineif "BROWSER" in os.environ:
. – Jeromewebbrowser
module's code where it was placed was never executed -- so try to figure out way that's the case. I don't know where you put the breakpoint, but perhaps"BROWSER"
wasn't inos.environ
... – Jeromewebbrowser
module. It's easy -- there's an example of doing this with thesubprocess
module in this answer to the question Opening several tabs in Firefox using Python. – Jeromesubprocess
in a different manner. Unfortunately we still don't know why setting the environment variable"BROWSER"
wasn't work properly... – Jerome