Skip to content

[Windows] pytest randomly fail with tkinter app initialization #10364

Open
@jnhyperion

Description

@jnhyperion

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: pytest-dev/py#103

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: help wanteddevelopers would like help from experts on this topictype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions