On this page
webbrowser
Prev: weakref | Up: stdlib_module | Next: winreg
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — webbrowser
Tags: #public #python3 #stdlib
Import
import webbrowser
Overview
Cross-platform helper that opens documentation or web pages in the user's default system browser (open, open_new, open_new_tab, get). Handy for local CLI tools and launching locally served documentation (such as a prettynb or spatial-ui workspace).
Quick Start
import webbrowser
controller = webbrowser.get()
assert hasattr(controller, "open_new_tab")
Implementation Walkthrough
1. Safe Launch with Scheme Validation & Headless Fallback
Validate the URL scheme before passing it to webbrowser and print the address as a fallback for headless or SSH terminals:
from urllib.parse import urlsplit
import webbrowser
def launch_docs(target: str) -> bool:
if urlsplit(target).scheme not in {"http", "https"}:
raise ValueError(f"Unsupported scheme: {target!r}")
return webbrowser.open_new_tab(target)
Gotchas & Boundaries
- Validate the URL scheme first: Only pass
httporhttpsURLs towebbrowser. - Headless / SSH environments:
open_new_tab()may returnFalseor fall back to a text browser; always print the URL to the terminal as well. - Calls are non-blocking:
webbrowserreturns immediately without waiting for the browser tab to load or close.
Common Functions
- webbrowser.get() —
get(using=None)— Return a browser launcher instance appropriate for the environment. - webbrowser.open() —
open(url, new=0, autoraise=True)— Display url using the default browser. - webbrowser.open_new() —
open_new(url)— Open url in a new window of the default browser. - webbrowser.open_new_tab() —
open_new_tab(url)— Open url in a new page ("tab") of the default browser. - webbrowser.register() —
register(name, klass, instance=None, *, preferred=False)— Register a browser connector.
Classes & Objects
- webbrowser.Error —
webbrowser.Errorclass. (Methods:.add_note(),.with_traceback())
Examples & Notes
Convenient web-browser controller:
import webbrowser
webbrowser.open_new_tab('https://docs.python.org/3/')
# Using a specific browser:
# browser = webbrowser.get('safari')
# browser.open_new_tab('https://www.python.org')
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/antigravity
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/weakref
- tuts/python3/stdlib_module/webbrowser/Error
- tuts/python3/stdlib_module/webbrowser/get
- tuts/python3/stdlib_module/webbrowser/open
- tuts/python3/stdlib_module/webbrowser/open_new
- tuts/python3/stdlib_module/webbrowser/open_new_tab
- tuts/python3/stdlib_module/webbrowser/register
- tuts/python3/stdlib_module/winreg
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/weakref Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/Error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/get Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/open_new Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/open_new_tab Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser/register Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winreg Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib