From 413e20f81c982f61384c0d9813053bc0afa6e7f7 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Fri, 24 Jan 2025 19:28:57 -0500 Subject: [PATCH] Adjust fix_display() to work when tkinter is not installed --- sciunit/utils.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sciunit/utils.py b/sciunit/utils.py index b48602a..276c779 100644 --- a/sciunit/utils.py +++ b/sciunit/utils.py @@ -193,16 +193,20 @@ def fix_display(self) -> None: backend must be changed to one that doesn't need a display. """ - try: - tkinter.Tk() - except (tkinter.TclError, NameError): # If there is no display. + if tkinter is not None: # tkinter is at least installed try: - import matplotlib as mpl - except ImportError: - pass + tkinter.Tk() + except (tkinter.TclError, NameError): + pass # It appears that there there is no display. else: - sciunit.logger.info("Setting matplotlib backend to Agg") - mpl.use("Agg") + return # We can use the tkinter backend. + try: + import matplotlib as mpl + except ImportError: + pass + else: + sciunit.logger.info("Setting matplotlib backend to Agg") + mpl.use("Agg") def load_notebook( self, name: str