Skip to content

Conversation

@kangmg
Copy link

@kangmg kangmg commented Jan 29, 2026

The _ environment variable is not guaranteed to exist in all environments. This PR replaces os.environ["_"] with os.environ.get("_", "") to prevent KeyError.

-         if os.environ["_"].find("jupyter") == -1:
+         if os.environ.get("_", "").find("jupyter") == -1:

if os.environ["_"].find("jupyter") == -1:

@kangmg
Copy link
Author

kangmg commented Jan 30, 2026

The suggested fix is a minimal change to prevent the "KeyError", but a more robust approach would be to detect the notebook environment directly using IPython:

def _in_notebook():
    try:
        from IPython import get_ipython
        return get_ipython() is not None
    except ImportError:
        return False

Then replace:

-    if os.environ["_"].find("jupyter") == -1:
+    if not _in_notebook():

This would be more robust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant