pythonubuntuwindows-subsystem-for-linuxpython-webbrowser

How is Python's webbrowser.open() method used on a Windows machine using a Linux subsystem?


Normally I work with Ruby and I don't have this issue thanks to some gems I have installed, but for Python I'm not sure how to handle this. I'm on a Windows machine, and I develop with VS Code using WSL/Ubuntu.

A simple script such as

import webbrowser
webbrowser.open('https://inventwithpython.com/')

simply does nothing and returns False with no other information. I know this means it is failing to open the browser, and I suspect it is due to the subsystem not having access (or not knowing how to access) the default browser that is set for the Windows OS. The problem is that this is a bit out of the scope of what I know how to do.

I've found a workaround in the following way...

import subprocess
subprocess.run(["cmd.exe", "/c", "start", "https://inventwithpython.com/"])

However I'm guessing this code won't work on all "normal" systems. I'd like to know if there's some other workaround I can use that still uses the .open() method mentioned above.


Solution

  • Issue solved. I asked a former teacher of mine, and they gave me a few commands to run in the terminal that fixed the issue, which were the following:

    echo "export BROWSER=\"/mnt/c/Program Files/Google/Chrome/Application/chrome.exe\"" >> ~/.zshrc
    echo "export GH_BROWSER=\"'/mnt/c/Program Files/Google/Chrome/Application/chrome.exe'\"" >> ~/.zshrc
    

    Thanks Yann!