To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call
webbrowser.open('https://google.com') to open Google. Don’t forget to run
import webbrowser first. But you don’t have to install the module because it’s already in Python’s standard library.
Here’s an example Python script that opens the URL
import webbrowser webbrowser.open('https://finxter.com/')
A new browser tab with your default browser (Chrome, Edge, Safari, Brave — whatever you set up as standard browser in your OS settings) opens, initialized with the URL provided as a string argument of the
About the Webbrowser Module
webbrowser module is already part of the Python Standard Library, so you can import it without needing to install it first.
You can also run the module from your command line or terminal by using the following command:
python -m webbrowser -t "https://finxter.com"
Good to know if you ever want to open a URL from your operating system command line or terminal (Windows, macOS, Linux, Ubuntu) because the fact that you use Python makes it portable and operating system independent!
You can specify additional arguments to get more control over which tab is opened by means of the new argument of the
webbrowser.open(url, new=0, autoraise=True)
new argument allows you to control the browser window:
- If you set
new=0(default), you open the URL in the same browser window.
- If you set
new=1, you open a new browser window.
- If you set
new=2, you open a new browser tab.
autoraise argument allows you to raise the window (default behavior).
Webbrowser Open in New Tab
A short way of opening a given URL in a new tab from your Python script is to call
webbrowser.open_new_tab() and pass your URL string as a single argument.
import webbrowser my_url = 'https://finxter.com' webbrowser.open_new_tab(my_url)
Select the Webbrowser
You can also return a controller object for a given browser by calling
webbrowser.get() and passing the browser type into it. Now, you can call the
open_new_tab() methods on this controller object to open the URL in your desired web browser.
import webbrowser webbrowser.get("chrome").open("https://finxter.com")
Here are the supported browser types:
|Type Name||Class Name|
Thanks for Reading ❤️
To keep learning, feel free to check out our email academy and download our free Python cheat sheets. 🙂
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.