Intermittent pytest failures complaining about missing tcl files even though the files are there

357 Views Asked by At

Tools: Python 3.10, cmd2 2.4.0, Windows 11, tk 0.1.0, matplotlib 3.5.1, pytest 7.0.1, cmd2-ext-test 2.0.0

I am building a set of pytest tests that cover code which normally outputs plots using matplotlib (backend="TkAgg"). This code is using cmd2 so the tests are calling cmd2 commands using cmd2-ext-test. In testing mode, I call plt.close('all') instead of plt.show() so that I don't have to manually close any windows while testing - that's the only difference in testing mode.

When running these tests, 1 or 2 out of 20 will randomly fail with an error that claims a file in tcl8.6 or tk8.6 doesn't exist even though it does (often a different file each time). Different tests fail, and sometimes none of them fail.

My Python packages are all installed for all users (so at c:/program files/python310/Lib using an administrator cmd prompt). Based on other answers online, I've tried copying the tcl/tcl8.6 and tcl/tk8.6 directories to Lib and no change. I tried commenting out the plt.close('all') and also no change.

The error is always triggered on a plt.figure() call. One example of the error I get when using pytest -vv (verbose mode) is shown below:

E             +     new_fig = plt.figure()
E             +   File "C:\Program Files\Python310\lib\site-packages\matplotlib\pyplot.py", line 787, in figure
E             +     manager = new_figure_manager(
E             +   File "C:\Program Files\Python310\lib\site-packages\matplotlib\pyplot.py", line 306, in new_figure_manager
E             +     return _backend_mod.new_figure_manager(*args, **kwargs)
E             +   File "C:\Program Files\Python310\lib\site-packages\matplotlib\backend_bases.py", line 3494, in new_figure_manager
E             +     return cls.new_figure_manager_given_figure(num, fig)
E             +   File "C:\Program Files\Python310\lib\site-packages\matplotlib\backends\_backend_tk.py", line 925, in new_figure_manager_given_figure
E             +     window = tk.Tk(className="matplotlib")
E             +   File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2299, in __init__
E             +     self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
E             + _tkinter.TclError: Can't find a usable init.tcl in the following directories:
E             +     {C:\Program Files\Python310\tcl\tcl8.6}
E             +
E             + C:/Program Files/Python310/tcl/tcl8.6/init.tcl: couldn't read file "C:/Program Files/Python310/tcl/tcl8.6/init.tcl": No error
E             + couldn't read file "C:/Program Files/Python310/tcl/tcl8.6/init.tcl": No error
E             +     while executing
E             + "source {C:/Program Files/Python310/tcl/tcl8.6/init.tcl}"
E             +     ("uplevel" body line 1)
E             +     invoked from within
E             + "uplevel #0 [list source $tclfile]"
E             +
E             +
E             + This probably means that Tcl wasn't installed properly.
E             +
E             + EXCEPTION of type 'TclError' occurred with message: Can't find a usable init.tcl in the following directories:
E             +     {C:\Program Files\Python310\tcl\tcl8.6}
E             +
E             + C:/Program Files/Python310/tcl/tcl8.6/init.tcl: couldn't read file "C:/Program Files/Python310/tcl/tcl8.6/init.tcl": No error
E             + couldn't read file "C:/Program Files/Python310/tcl/tcl8.6/init.tcl": No error
E             +     while executing
E             + "source {C:/Program Files/Python310/tcl/tcl8.6/init.tcl}"
E             +     ("uplevel" body line 1)
E             +     invoked from within
E             + "uplevel #0 [list source $tclfile]"
E             +
E             +
E             + This probably means that Tcl wasn't installed properly.

I'm totally stumped! Any help would be greatly appreciated!

0

There are 0 best solutions below