webbrowser.py reads the environment variable "BROWSER" to find executable using "_synthesize".
This does not work with macOS, where the browser is open by either
It might be better to tweak the code that calls _synthesize, using the already registered browsers when available (which would lead to using the registration for MacOSXOSAScript at the start of register_standard_browsers. That likely needs changes as well because this currently tries to start chrome using the following shell command:
osascript -e 'tell application "chrome" activate open location "https://www.python.org" end'
I don't have Chrome installed, could you check if this command opens the python website for you? If not, try the same with "chrome" replaced by "google chrome".
The diff below seems to work, but I haven't fully thought through the consequences on platforms other than macOS. That's why I haven't created a PR at this time.
diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
index 636e8ca459..310c6d58ce 100755
--- a/Lib/webbrowser.py+++ b/Lib/webbrowser.py@@ -114,7 +114,21 @@ def _synthesize(browser, *, preferred=False):
executable for the requested browser, return [None, None].
"""
- cmd = browser.split()[0]+ parts = browser.split()+ if len(parts) == 1:+ # Assume this the name of a registered command, use+ # that unless it is a GenericBrowser.+ try:+ command = _browsers[browser.lower()]+ except KeyError:+ pass++ else:+ if not isinstance(command[1], GenericBrowser):+ _tryorder.insert(0, browser.lower())+ return [None, command[1]]++ cmd = parts[0]
if not shutil.which(cmd):
return [None, None]
name = os.path.basename(cmd)
@@ -466,9 +480,10 @@ def register_standard_browsers():
if sys.platform == 'darwin':
register("MacOSX", None, MacOSXOSAScript('default'))
- register("chrome", None, MacOSXOSAScript('chrome'))+ register("chrome", None, MacOSXOSAScript('google chrome'))
register("firefox", None, MacOSXOSAScript('firefox'))
register("safari", None, MacOSXOSAScript('safari'))
+ register("safari-preview", None, MacOSXOSAScript('safari technology preview'))
# OS X can use below Unix support (but we prefer using the OS X
# specific stuff)
Ignore the registration of "safari-preview", I added that because that's the only alternative browser on the system I'm working on. A PR for this issue should not contain that.
If the environment variable BROWSER exists, it is interpreted as the os.pathsep-separated list of browsers to try ahead of the platform defaults. 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. 1
Any update or recommended workaround for this? @sobolevn's osascript works for me by itself, and from within a script, but the webbrowser module doesn't seem to use it even when it's on my PATH.
Activity
ronaldoussoren commented on Dec 28, 2023
It might be better to tweak the code that calls _synthesize, using the already registered browsers when available (which would lead to using the registration for MacOSXOSAScript at the start of register_standard_browsers. That likely needs changes as well because this currently tries to start chrome using the following shell command:
I don't have Chrome installed, could you check if this command opens the python website for you? If not, try the same with "chrome" replaced by "google chrome".
sobolevn commented on Dec 28, 2023
@ronaldoussoren I have chrome installed on macos, it does not work for me:
» osascript -e 'tell application "chrome" activate open location "https://www.python.org" end' 52:60: execution error: Cannot open application "chrome". (-1728)But, this one works:
» osascript -e 'tell application "google chrome" activate open location "https://www.python.org" end'Also works with: firefox, safari (I don't have any other browsers).
ronaldoussoren commented on Dec 28, 2023
The diff below seems to work, but I haven't fully thought through the consequences on platforms other than macOS. That's why I haven't created a PR at this time.
Ignore the registration of "safari-preview", I added that because that's the only alternative browser on the system I'm working on. A PR for this issue should not contain that.
pythongh-113539: Enable using ``$BROWSER`` to reorder default seach o…
ronaldoussoren commented on Dec 29, 2023
I've added a PR and I'm being very conservative with this: the PR phrasing and documentation changes assume this is a new feature and not a bug fix.
The reason is that the documentation for webbrowser module mentions that values in the $BROWSER search list refer to command-lines:
micseydel commented on Apr 18, 2025
Any update or recommended workaround for this? @sobolevn's osascript works for me by itself, and from within a script, but the webbrowser module doesn't seem to use it even when it's on my PATH.
gh-113539: Enable using ``$BROWSER`` to reorder default seach order i…
hugovk commented on Apr 25, 2025
This will be in the next release (3.14.0b1) in a couple of weeks.
micseydel commented on Apr 25, 2025
@hugovk thanks for the update. Is there a recommended workaround for affected versions?
hugovk commented on Apr 26, 2025
No official recommendation, you could patch the module.