Erik
Erik

Reputation: 7731

Accessing environment variables in python

Why does this not print out 'xxx'?

$ SECRET_KEY='xxx' python -c 'import os; print os.environ['SECRET_KEY']'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'SECRET_KEY' is not defined

Upvotes: 0

Views: 112

Answers (1)

mgilson
mgilson

Reputation: 309919

You're shell quoting is off and as a result, python sees:

import os; print os.environ[SECRET_KEY]

(note the missing quotes). This should work:

SECRET_KEY='xxx' python -c "import os; print os.environ['SECRET_KEY']"

should work.

Upvotes: 3

Related Questions