Skip to content
← Back to Community
Python Environment Variables - os.environ returns None
Profile icon
hardbody

I set a secret key as an environment variable
Screenshot 2021-05-07 at 9.40.07 AM
and I have a pyfile called test.py that calls it

import os SECRET_KEY = os.environ['SECRET_KEY'] print('the secret key is {}'.format(SECRET_KEY))

When I run it with .replit it works fine

language="python" run="python test.py"
> python test.py the secret key is key_that_is_secret

But when I run it from the shell
~/pythonapp$ python test.py
it returns an error

Traceback (most recent call last): File "test.py", line 3, in <module> SECRET_KEY = os.environ['SECRET_KEY'] File "/usr/lib/python3.8/os.py", line 675, in __getitem__ raise KeyError(key) from None KeyError: 'SECRET_KEY'

Is there something I'm missing?

Voters
Profile icon
hardbody
Comments
hotnewtop
Profile icon
ManuelSandoval2

Instead of using os.environ try using os.environ.get('SECRET_KEY') i did have the same error and now it seems to work, still have some other issues though

Profile icon
SixBeeps

My guess is that environment variables get added whenever the run button is clicked, which would be a little strange, but I'm not sure. Possible bug?