From d8873f4cf601833e12c0f734f27b9bca229a4428 Mon Sep 17 00:00:00 2001 From: Mehrtash Babadi Date: Wed, 11 Sep 2024 22:13:12 +0000 Subject: [PATCH 1/2] fix for interactive env testing --- cellarium/cas/settings.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/cellarium/cas/settings.py b/cellarium/cas/settings.py index 44e2f09..f91a391 100644 --- a/cellarium/cas/settings.py +++ b/cellarium/cas/settings.py @@ -14,19 +14,24 @@ def is_interactive_environment() -> bool: :return: True if the current environment is interactive, False otherwise """ + import sys + + import __main__ + + # Check if running in an interactive shell (e.g., IPython, Jupyter) + if hasattr(sys, "ps1"): + return True + # Check if running in a context without a '__file__' attribute, typical of interactive environments + if not hasattr(__main__, "__file__"): + return True + # Check specific interactive environment conditions try: + # Check if running in IPython or Jupyter by checking the module name from IPython import get_ipython - if "IPKernelApp" not in get_ipython().config: - # Running in a non-interactive environment. - return False - else: - if "terminal" in get_ipython().config["IPKernelApp"]["connection_file"]: - # Running in an IPython terminal - return True - else: - # Running in a Jupyter environment. - return True - except (ImportError, AttributeError): - # Running in a non-interactive environment. - return False + shell = get_ipython().__class__.__name__ + if shell in ["ZMQInteractiveShell", "TerminalInteractiveShell"]: + return True + except NameError: + pass + return False From a52220c565aca2bb80887b7a1442e538235f8c4a Mon Sep 17 00:00:00 2001 From: Mehrtash Babadi Date: Thu, 12 Sep 2024 15:09:34 +0000 Subject: [PATCH 2/2] fix IPython import issue --- cellarium/cas/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cellarium/cas/settings.py b/cellarium/cas/settings.py index f91a391..455429e 100644 --- a/cellarium/cas/settings.py +++ b/cellarium/cas/settings.py @@ -32,6 +32,6 @@ def is_interactive_environment() -> bool: shell = get_ipython().__class__.__name__ if shell in ["ZMQInteractiveShell", "TerminalInteractiveShell"]: return True - except NameError: + except (ModuleNotFoundError, NameError): pass return False