0

I've set the environment variable in the shell like this

export DEPLOY=development 

and running this returns "development"

echo $DEPLOY 

However... this returns none in python.

import os os.environ.get("DEPLOY") 

and this raises a key error

os.environ["DEPLOY"] 

I've deleted the .pyc files and restarted my IDE...

Any ideas?

3
  • If Python is not a child of the process where you ran the export, of course it can't inherit the variable from that. Commented Nov 16, 2020 at 14:24
  • So define the variable in the working directly of the python code? Commented Nov 16, 2020 at 14:51
  • If you mean "working directory" then no, that's not at all what "parent process" means. If you run Python from the shell prompt where you previously executed the export, that Python instance does get a copy of the variable (regardless of which directory you are running it in) as long as you didn't exit that shell in the meantime. (This is all assuming you are not on Windows, where the mechanics are more twisty and perverse.) Commented Nov 16, 2020 at 14:53

1 Answer 1

1

export DEPLOY=development only changes the environment for your shell, and programs that shell starts.

Because you're starting Python from your IDE, not from the shell, the Python interpreter is not a child (or grandchild) process of that shell, and so does not inherit the shell's environment variables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks understand this completly now, but still may help someone else in the future. Cheers for the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.