On this page
  1. webbrowser
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Safe Launch with Scheme Validation & Headless Fallback
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects
  10. Examples & Notes

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

Common Functions

Classes & Objects

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

Related notes