How to set BROWSER environmental variable for python webbrowser
Asked Answered
F

2

5

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?
Fodder answered 16/9, 2014 at 17:33 Comment(7)
Assuming the path given to the executable is valid, what you have should work. The source for the webbrowser module is included with the Python distribution and is in C:\PythonX\Lib\webbrowser.py, so you could make a copy of it for debugging purposes to find the problem. Look for the line if "BROWSER" in os.environ:.Jerome
thank you that is very useful info. looking at the source (I've included it above ) I can't see an obvious problem, although I'm suspecting it has to do with the register function . I tried to set a breakpoint within the webbrowser module code itself, but this does not cause a break. Is this possible , how do you step through more complex code with python? - BillFodder
Assuming you set the breakpoint properly, it simply sounds like the webbrowser 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 in os.environ...Jerome
A different approach would be to just start Firefox yourself, rather than using the webbrowser module. It's easy -- there's an example of doing this with the subprocess module in this answer to the question Opening several tabs in Firefox using Python.Jerome
Martineau, thanks for your help. I ended up getting an answer here : #25850313. Regards, - BillFodder
Bill: Good...same basic idea really, just uses subprocess in a different manner. Unfortunately we still don't know why setting the environment variable "BROWSER" wasn't work properly...Jerome
@user61629 Would be nice if you can check if reordering would work for you.Freberg
F
6

Tried your example and got the same result: Was opening in IE, not in Firefox. Reason is, that at import time of webbrowser, the BROWSER environment variable is not yet set. By simply reordering:

import os
# put it **before** importing webbroser
os.environ["BROWSER"] = "C:\\FirefoxPortable\\FirefoxPortable.exe"
import webbrowser
# from subprocess import call

webbrowser.open('http://google.com')

it works now. I figured that by trying to set the environment variable on the command line. Note: It did not work having the path in quotes

set BROWSER=C:\FirefoxPortable\FirefoxPortable.exe

did work,

set BROWSER="C:\FirefoxPortable\FirefoxPortable.exe"

did not. Sorry for the late answer, but the diagnostics with

>>> webbrowser._browsers
>>> webbrowser._tryorder

had been very helpful, thanks.

Freberg answered 1/9, 2015 at 10:54 Comment(3)
I'll Have to look into this later, but thanks for the response.Fodder
set "BROWSER=C:\FirefoxPortable\FirefoxPortable.exe" should also work!Sherr
thanks for your tip on webbrowser._browsers and webbrowser._tryorder. Was very useful since registering webbrowser to use ms edge was not sufficient to change default browser and had to reverse tryorder to make it become the default browser (first in list for subsequent modules using webbrowser such as bokeh-server and panel. useage syntax : webbrowser._tryorder.reverse()Nicolenicolea
G
3

Try the following code:

webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'))
a=webbrowser.get('firefox')
a.open("www.google.com")­­­­­
Gerlachovka answered 27/5, 2015 at 13:43 Comment(1)
Please provide some accompanying description and code quoting of your answerSadler

© 2022 - 2024 — McMap. All rights reserved.