1

Quick question: I created a new environment var in the bash shell (using ubuntu) via export NUKE_GIZMO_PATH=/home/... which seems to work as it is shown via printenv and echo $NUKE_GIZMO_PATH, however it doesn't seem to exist for python as

import os
print(os.environ['NUKE_....']) returrns a keyerror and 

print(os.environ.get('NUKE_...')) returns none.

Here is my question: Why? ;D

EDIT: Closing and opening a new terminal seems to destroy the var?

Thanks and have a nice day.

Jan
  • 88
  • 5
  • Environment is scoped to a process, and inherited by children. When that process exits, its environment variables are gone (unless it still has children running, and then it's only those children that have them). – Charles Duffy May 20 '18 at 17:22
  • ...that said, right now the question doesn't include reproducer steps. `export NUKE_GIZMO_PATH=/home/...; python -c 'import os; print os.environ["NUKE_GIZMO_PATH"]'` certainly does work, so your problem currently doesn't meet the [mcve] definition (requiring that you give someone else exact steps they can follow, ideally just via copy/paste, to cause the issue themselves). – Charles Duffy May 20 '18 at 17:23

1 Answers1

0

As long as the var has been exported, it will exist for the session / process in which it was defined, and child processes created from it.

But as you noted, all vars go away when the session / process ends, and need to be re-established.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
  • *If* this answers the question, the question is presumably a duplicate of [How to permanently export a variable in Linux?](https://stackoverflow.com/questions/13046624/how-to-permanently-export-a-variable-in-linux) or another question that isn't Python-specific at all. – Charles Duffy May 20 '18 at 17:25
  • The question was "why", not "how to avoid". – Chris Johnson May 20 '18 at 17:27
  • Even then, it's in no way specific to Python, so this answer applies to any other "why is an environment variable set in one window not available in any other?" question -- which we surely have duplicates for. If you truly believe that this is what the OP was asking for, you should be casting a close-as-dupe vote. – Charles Duffy May 20 '18 at 17:34