Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "tcgetpgrp failed: Not a tty" using python3 to open web browser

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?

like image 610
Gharbad The Weak Avatar asked May 15 '26 07:05

Gharbad The Weak


2 Answers

Short answer (confirmed)

export BROWSER=/mnt/c/path/to/windows/browser before starting Python, but ...

See other answer (also confirmed)

While this solves the original question that was asked, it does not work for file:/// URIs where the file in inside the WSL filesystem.

This answer from NeoMatrixJR solves both the original problem and also works with file:/// URIs.

More detail

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 the BROWSER environment variable first, but falls back to (I believe) xdg-open.

  • xdg-open uses whatever browser is defined in the alternatives system for x-www-browser or www-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 a file viewer on WSL that allows you to open files and folders from WSL in Windows and a fake web browser that allows opening urls in your default browser on Windows 10.

  • wslview is registered during the wslu installation as the alternative for both x-www-browser and www-browser.

  • webbrowser.open doesn't just call xdg-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, the tcgetpgrp system call. According to the tcgetpgrp manpage:

    The function tcgetpgrp() returns the process group ID of the foreground process group on the terminal associated to fd, which must be the controlling terminal of the calling process.

  • Here's where I have to "hand-wave" a bit -- Something in the hand-off from webbrowser.open to wslview to binfmt_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. For example, for Edge:

    export BROWSER='/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
    

    Credit to @rahul13579 for supplying the Edge location in a subsequent answer which was, unfortunately, not-an-answer and has been deleted.

  • 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.

like image 148
NotTheDr01ds Avatar answered May 17 '26 20:05

NotTheDr01ds


Looks like if you install wslu manually on 22.04+ you can also export BROWSER=/usr/bin/wslview to resolve this.

like image 24
NeoMatrixJR Avatar answered May 17 '26 21:05

NeoMatrixJR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!