python-build-standalone: Not able to run a tkinter program

I am using the bazel/rules_python to compile our python code. according to the configuration, we are using the cpython-3.9.13+20220802-x86_64-unknown-linux-gnu-install_only.tar.gz as our python interpreter. But as we are using the Tkinter, there is a segment fault during the runtime. the code

import faulthandler
faulthandler.enable()

import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame,text="Exit",command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()

the stack trace

/tmp/python_interpreter_x86_64-unknown-linux-gnu ❯ ./bin/python3.9 tkinter_test.py                                                                                                                                                                                            
Fatal Python error: Segmentation fault

Current thread 0x00007fe29108ef40 (most recent call first):
  File "/tmp/python_interpreter_x86_64-unknown-linux-gnu/lib/python3.9/tkinter/__init__.py", line 2572 in __init__
  File "/tmp/python_interpreter_x86_64-unknown-linux-gnu/lib/python3.9/tkinter/__init__.py", line 3148 in __init__
  File "/tmp/python_interpreter_x86_64-unknown-linux-gnu/tkinter_test.py", line 9 in <module>
[1]    55139 segmentation fault (core dumped)  ./bin/python3.9 tkinter_test.py

according to other tickets, seems the python interpreter is already linked to the Tkinter.

Update

I was trying to use the debug version to know what happened in the core dump. but it is very strange that I am able to run this with python3.9d. it is very weird.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 33 (5 by maintainers)

Most upvoted comments

Let’s keep conversation here focused on the specific bug here rather than uv Python installation behaviors / pyenv / etc. please.

@alexis-massa

I did try to uv python install --reinstall 3.13.

Remove the virtual environment and install python with some other tool like pyenv. Then use

uv run -p 3.13 --no-managed-python

Future runs will use the version from the newly created venv I’m pretty sure.

No, I don’t think that commit has been merged. It’s on an open branch.

Someone commented that this was not a problem on MS-Windows. I wanted to say I get the what seems to be the problem on MS-Windows.

❯ uvx --version
uv-tool-uvx 0.6.11 (0632e24d1 2025-03-30)
❯ uvx .\my_tkinter_app-0.1.2-py3-none-any.whl
Installed 3 packages in 1.22s
Traceback (most recent call last):
 File "<frozen runpy>", line 198, in _run_module_as_main
 File "<frozen runpy>", line 88, in _run_code
 File "C:\Users\rnwol\AppData\Local\uv\cache\archive-v0\u-Iv1TOsu8C9Esopv3qX4\Scripts\my-tkinter-app.exe\__main__.py", line 10, in <module>
   sys.exit(main())
            ~~~~^^
 File "C:\Users\rnwol\AppData\Local\uv\cache\archive-v0\u-Iv1TOsu8C9Esopv3qX4\Lib\site-packages\my_tkinter_app\app.py", line 24, in main
   controller = MainController()
 File "C:\Users\rnwol\AppData\Local\uv\cache\archive-v0\u-Iv1TOsu8C9Esopv3qX4\Lib\site-packages\my_tkinter_app\controller\main_controller.py", line 7, in __init__
   self.view = MainView(self)
               ~~~~~~~~^^^^^^
 File "C:\Users\rnwol\AppData\Local\uv\cache\archive-v0\u-Iv1TOsu8C9Esopv3qX4\Lib\site-packages\my_tkinter_app\view\main_view.py", line 5, in __init__
   super().__init__()
   ~~~~~~~~~~~~~~~~^^
 File "C:\Users\rnwol\AppData\Roaming\uv\python\cpython-3.13.0-windows-x86_64-none\Lib\tkinter\__init__.py", line 2459, in __init__
   self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
             ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
   C:/Users/rnwol/AppData/Roaming/uv/python/cpython-3.13.0-windows-x86_64-none/lib/tcl8.6 C:/Users/rnwol/AppData/Roaming/uv/python/lib/tcl8.6 C:/Users/rnwol/AppData/Roaming/uv/lib/tcl8.6 C:/Users/rnwol/AppData/Roaming/uv/python/library C:/Users/rnwol/AppData/Roaming/uv/library C:/Users/rnwol/AppData/Roaming/uv/tcl8.6.12/library C:/Users/rnwol/AppData/Roaming/tcl8.6.12/library

But if I specify the python version installed from https://www.python.org/downloads/

❯ uvx -p "C:\Python313\python.exe" .\my_tkinter_app-0.1.3-py3-none-any.whl
Installed 3 packages in 1.25s

The application is a basic TKinter app. https://github.com/rnwolf/my_tkinter_app

I fixed this with ff0ee27d1b2fb55d134efb19c0c0440fcf54e14f but am not happy with the approach

cc @geofft perhaps you have an idea of how to make that better? I think, at the very least, we should generate the contents replacing the ALL exclusion.

After a git bisect testing against OP code, I found that this commit is the first bad commit. I have no idea about how to fix this though !

I was able to repro this as well with python-build-standalone on 3.10 - 3.13.

Repro

main.py

import os
import tkinter
worker_pid = os.fork()
if worker_pid == 0:
    import subprocess
    subprocess.run(["/usr/bin/whoami"], capture_output=True, encoding="utf-8", check=True)
else:
    pid, status = os.waitpid(worker_pid, 0)
    if not os.WIFEXITED(status):
        print(f"ungraceful exit with signal {os.WTERMSIG(status)}")

With a managed python:

rm -rf .venv pyproject.toml
uv init --python 3.13
uv venv --python-preference only-managed
source .venv/bin/activate
python3 main.py

Behavior - crash:

ungraceful exit with signal 5

With a system python installed via brew install python@3.13

rm -rf .venv pyproject.toml
uv init --python 3.13
uv venv --python-preference only-system
source .venv/bin/activate
python3 main.py

Expected behavior (no crash)

Traceback (most recent call last):
  File "/Users/peter/work/tkinter-repro/main.py", line 2, in <module>
    import tkinter
  File "/opt/homebrew/Cellar/python@3.13/3.13.4/Frameworks/Python.framework/Versions/3.13/lib/python3.13/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'

@linde12 i manage my dependencies with uv but my python installations with chocolatey on windows and on linux with pyenv. First i install pyenv set the string for the fish shell in the config and then i do the rest with uv and it works. I had this tkinter problem only on the Linux Systems

@rezzubs Ah!

Aha, uv run -p 3.13 --python-preference only-managed test.py does not work either, but with -p 3.8 it worked.

Image

No tcl error (which i’ve gotten before and worked around using TCL_LIBRARY env var) but reinstalling python 3.8 via uv python uninstall 3.8 and then just running uv run -p 3.8 --python-preference only-managed test.py again broke it.

Perhaps i had a version of py 3.8 that was working, but after uninstalling and letting uv grab the latest 3.8 release it breaks. So basically only my system python & older (?) releases of python-build-standalone are working? 😦

EDIT: I saw that cpython-3.8.20-linux-x86_64-gnu does not work, however 3.8.2 - 3.8.12 are working!

Image

EDIT 2: Can confirm that 3.10.2 & (released in 20220227) works, but so does 3.10.3 (released in 20220318) which was released in the same release as 3.8.13 which does not work!

So two versions from the same release are inconsistent, 3.10.2 works whereas 3.8.13 from same release does not. I am very confused!

Also encountering this, using Arch (btw), but it is working with python 3.8 and 3.13 (none of which i can use in my project)

test.py:

import tkinter


def main():
    root = tkinter.Tk()
    button = tkinter.Button(root, text="Click Me")
    button.pack(padx=20, pady=20)
    root.mainloop()


if __name__ == "__main__":
    main()
  • uv run -p 3.8 test.py - OK
  • uv run -p 3.9 test.py - Fail
  • uv run -p 3.10 test.py - Fail
  • uv run -p 3.11 test.py - Fail
  • uv run -p 3.12 test.py - Fail
  • uv run -p 3.13 test.py - OK

Just confirming this is still reproducible after upgrading tcl/tk (#495)

❯ uv version
uv 0.5.19
❯ uv python list --show-urls | grep cpython-3.9
cpython-3.9.21-linux-x86_64-gnu                   https://github.com/astral-sh/python-build-standalone/releases/download/20250115/cpython-3.9.21%2B20250115-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz
❯ uv python install --reinstall 3.9 3.13
Installed 2 versions in 3.60s
 ~ cpython-3.9.21-linux-x86_64-gnu
 ~ cpython-3.13.1-linux-x86_64-gnu

❯ uv run -v -p 3.9 script.py
DEBUG uv 0.5.19
DEBUG No project found; searching for Python interpreter
DEBUG Searching for Python 3.9 in virtual environments, managed installations, or search path
DEBUG Searching for managed installations at `/home/zb/.local/share/uv/python`
DEBUG Skipping incompatible managed installation `cpython-3.13.1-linux-x86_64-gnu`
DEBUG Skipping incompatible managed installation `cpython-3.12.8-linux-x86_64-gnu`
DEBUG Found managed installation `cpython-3.9.21-linux-x86_64-gnu`
DEBUG Found `cpython-3.9.21-linux-x86_64-gnu` at `/home/zb/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/bin/python3.9` (managed installations)
DEBUG Using Python 3.9.21 interpreter at: /home/zb/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/bin/python3.9
DEBUG Running `python script.py`
[xcb] Unknown sequence number while appending request
[xcb] You called XInitThreads, this is not your fault
[xcb] Aborting, sorry about that.
python3.9: xcb_io.c:157: append_pending_request: Assertion `!xcb_xlib_unknown_seq_number' failed.
Fatal Python error: Aborted

Current thread 0x00007d0a0c6d6740 (most recent call first):
  File "/home/zb/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/tkinter/__init__.py", line 2572 in __init__
  File "/home/zb/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/tkinter/__init__.py", line 3148 in __init__
  File "/fast/workspace/python-build-standalone/script.py", line 9 in <module>
DEBUG Command exited with signal: Some(6)

❯ uv run -v -p 3.13 script.py
DEBUG uv 0.5.19
DEBUG No project found; searching for Python interpreter
DEBUG Searching for Python 3.13 in virtual environments, managed installations, or search path
DEBUG Searching for managed installations at `/home/zb/.local/share/uv/python`
DEBUG Found managed installation `cpython-3.13.1-linux-x86_64-gnu`
DEBUG Found `cpython-3.13.1-linux-x86_64-gnu` at `/home/zb/.local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/bin/python3.13` (managed installations)
DEBUG Using Python 3.13.1 interpreter at: /home/zb/.local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/bin/python3.13
DEBUG Running `python script.py`
[xcb] Unknown sequence number while appending request
[xcb] You called XInitThreads, this is not your fault
[xcb] Aborting, sorry about that.
python3.13: xcb_io.c:157: append_pending_request: Assertion `!xcb_xlib_unknown_seq_number' failed.
Fatal Python error: Aborted

Current thread 0x00007df5d281d740 (most recent call first):
  File "/home/zb/.local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/lib/python3.13/tkinter/__init__.py", line 2772 in __init__
  File "/home/zb/.local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/lib/python3.13/tkinter/__init__.py", line 3361 in __init__
  File "/fast/workspace/python-build-standalone/script.py", line 9 in <module>
DEBUG Command exited with signal: Some(6)

Thanks for the report.

Can confirm this fails on the latest 20221107 distributions. The x86_64-unknown-linux-gnu distribution fails for me on the >>> label = tkinter.Label(frame, text="Hello, World") line:

>>> label = tkinter.Label(frame, text="Hello, World")
[xcb] Unknown sequence number while appending request
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python3: ../../src/xcb_io.c:157: append_pending_request: Assertion `!xcb_xlib_unknown_seq_number' failed.
Fatal Python error: Aborted

Current thread 0x00007f7f676b5c40 (most recent call first):
  File "/home/gps/src/python-build-standalone/dist/python/lib/python3.9/tkinter/__init__.py", line 2572 in __init__
  File "/home/gps/src/python-build-standalone/dist/python/lib/python3.9/tkinter/__init__.py", line 3148 in __init__
  File "<stdin>", line 1 in <module>
Aborted (core dumped)

This program works with other Python builds on my system. So something is amiss.

The xcb warnings point to a potential interaction with that library. I reckon we’re missing a configure flag somewhere in the X11 libraries which results in something using a non-standard config. tk and/or CPython is probably making assumptions about some X library config that aren’t true.

There’s possibly an upstream bug or two to report in here somewhere. But I don’t know what until root cause is understood.