Updated environment variable but os.getenv() keeps returning None

aselya picture aselya · Nov 21, 2016 · Viewed 32.8k times · Source

I can't seem to get my code to respond to custom environmental variables so I wrote a piece of code to test it. getenv is not pulling the environmental variables that I've set in BASH into my python code. Any help you can provide will be very appreciated.

$ FRUSTRATION="PYTHON!!"
$ echo FRUSTRATION
PYTHON!!
export FRUSTRATION
ipython
import os
very_frustrated = os.getenv("FRUSTRATION")
print(very_frustrated)
None

Answer

John Szakmeister picture John Szakmeister · Nov 21, 2016

Works for me:

:: export FOO=boo
:: python
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('FOO')
'boo'
>>>

My guess is that you either forgot to export the variable, or you spelled the variable wrong.