简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM