pytest-dev / pytest-dev/pytest

[Windows] pytest randomly fail with tkinter app initialization

Open
#10,364 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: help wanted type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

test env:

python: 3.8, 3.9, 3.10
pytest: 7.1.3
platform: Windows 10 pro

# test.py
import tkinter
import unittest


def create_app():
    app = tkinter.Tk()
    app.destroy()


class TestCaseMeta(type):
    def __new__(mcs, name, bases, tests):

        for i in range(10000):
            tests[f"test{i}"] = lambda _: create_app()

        return type.__new__(mcs, name, bases, tests)


class DemoTests(unittest.TestCase, metaclass=TestCaseMeta):
    pass


if __name__ == '__main__':
    unittest.main()

run python test.py it will never fail

run pytest -x test.py test will randomly fail

test.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test.py:6: in create_app
    app = tkinter.Tk()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <tkinter.Tk object .>, screenName = None, baseName = 'pytest', className = 'Tk', useTk = 1, sync = 0, use = None

    def __init__(self, screenName=None, baseName=None, className='Tk',
                 useTk=1, sync=0, use=None):
        """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
        be created. BASENAME will be used for the identification of the profile file (see
        readprofile).
        It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
        is the name of the widget class."""
        self.master = None
        self.children = {}
        self._tkloaded = 0
        # to avoid recursions in the getattr code in case of failure, we
        # ensure that self.tk is always _something_.
        self.tk = None
        if baseName is None:
            import os
            baseName = os.path.basename(sys.argv[0])
            baseName, ext = os.path.splitext(baseName)
            if ext not in ('.py', '.pyc'):
                baseName = baseName + ext
        interactive = 0
>       self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
E       _tkinter.TclError: Can't find a usable tk.tcl in the following directories:
E           c:/python/tcl/tcl8.6/tk8.6 c:/python/tcl/tk8.6 c:/lib/tk8.6 c:/lib/tk8.6 c:/lib/tk8.6 c:/library
E
E       c:/python/tcl/tk8.6/tk.tcl: couldn't read file "c:/python/tcl/tk8.6/ttk/scale.tcl": no such file or directory
E       couldn't read file "c:/python/tcl/tk8.6/ttk/scale.tcl": no such file or directory
E           while executing
E       "source [file join $::ttk::library scale.tcl]"
E           (file "c:/python/tcl/tk8.6/ttk/ttk.tcl" line 103)
E           invoked from within
E       "source c:/python/tcl/tk8.6/ttk/ttk.tcl"
E           ("uplevel" body line 1)
E           invoked from within
E       "uplevel \#0 [list source $::ttk::library/ttk.tcl]"
E           (file "c:/python/tcl/tk8.6/tk.tcl" line 689)
E           invoked from within
E       "source c:/python/tcl/tk8.6/tk.tcl"
E           ("uplevel" body line 1)
E           invoked from within
E       "uplevel #0 [list source $file]"
E
E
E       This probably means that tk wasn't installed properly.

c:\python\lib\tkinter\__init__.py:2261: TclError

looks like running pytest -s -x test.py this error will disappear, perhaps related to: https://github.com/pytest-dev/py/issues/103

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing test.py with pytest -x on the stated Windows and Python versions, then compare it with pytest -s -x and review the tkinter traceback in c:\python\lib\tkinter_init_.py. Done means the cause of the intermittent Tk initialization failure is identified and a regression check demonstrates stable behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.