Here’s the breakdown of my Windows WSL environment:
- Windows 11
- WSL version 2
- Ubuntu version 20.04.3 LTS
- Python 3.8.10
I have a super simple Python program I’m using to open a web page in my default browser.
Here is my code:
import webbrowser
webbrowser.open('https://github.com')
When I run this from my terminal the webpage opens up as expected, but I also get this error in the terminal:
tcgetpgrp failed: Not a tty
When my terminal displays this message, the cursor goes down to the next line and it looks like a process is hung or something. To be able to use the terminal I have to Ctrl+C to get it to give me the command prompt.
I looked for answers and everything I could find has to do with using Jupyter or PHP but I’m not using either of them, I’m just using plain old Python to try and open the browser.
Can anyone tell me what the issue is here and how to fix this/prevent it from happening?
2
Answers
Yes, I can also reproduce it from the Python (and IPython) REPL on Ubuntu under WSL. I don’t get the "lockup" that requires Ctrl+C when running interactively, at least.
I’ll theorize on the "why". Most of this I can confirm myself, but the last bullet below is still a bit of a mystery to me:
webbrowser-open
uses whatever browser is defined by theBROWSER
environment variable first, but falls back to (I believe)xdg-open
.xdg-open
uses whatever browser is defined in thealternatives
system forx-www-browser
orwww-browser
.On Ubuntu 20.04 on WSL, the
wslu
package is installed by default (it is no longer a default package under 22.04, though).That package includes the
wslview
helper. From its manpage:wslview
is registered during thewslu
installation as the alternative for bothx-www-browser
andwww-browser
.webbrowser.open
doesn’t just callxdg-open
, but it attempts to get the process information of the resulting browser so that it can (at the least) raise the window if requested. Part of this is obtaining the process group via, apparently, thetcgetpgrp
system call. According to thetcgetpgrp
manpage:Here’s where I have to "hand-wave" a bit — Something in the hand-off from
webbrowser.open
towslview
tobinfmt_misc
(the kernel system that allows it to launch Windows executables) is "losing" or redirecting a file descriptor of the terminal, resulting in this message.It appears to me to be a bug (unintended side-effect?) of
wslview
, since making sure it isn’t used will prevent the error from occurring.As a workaround, either:
export BROWSER=/mnt/c/path/to/windows/browser
before starting Python. Note that I’m not sure how to point to Edge, since it’s there’s no ".exe" for it that I’m aware of (it’s a Universal/Modern/UWP/whatever app).Or, since you are on Windows 11, install a Linux browser. I used Vivaldi to test and confirm that it opened properly from Python under WSL. Note that you can’t
sudo apt install
either Chromium or Firefox under WSL since they are both Snaps.Looks like if you install wslu manually on 22.04+ you can also export BROWSER=/usr/bin/wslview to resolve this.