Skip to content

Fix KeyError when _ environment variable is not set - #326

Merged
qzhu2017 merged 1 commit into
MaterSim:masterfrom
kangmg:master
Jan 31, 2026
Merged

Fix KeyError when _ environment variable is not set#326
qzhu2017 merged 1 commit into
MaterSim:masterfrom
kangmg:master

Conversation

@kangmg

Copy link
Copy Markdown
Contributor

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:

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

@kangmg

Copy link
Copy Markdown
ContributorAuthor

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:
fromIPythonimportget_ipythonreturnget_ipython() isnotNoneexceptImportError:
returnFalse

Then replace:

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

This would be more robust.

@qzhu2017
qzhu2017 merged commit 681edc7 into MaterSim:masterJan 31, 2026
Sign up for freeto 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.

2 participants

@kangmg@qzhu2017