Ask coding questions

← Back to all posts
Python Environment Variables - os.environ returns None

I've set a SECRET_KEY as an environment variable

and I have a short script called that calls it:

import os

SECRET_KEY = os.environ['SECRET_KEY']

print('the secret key is: {}'.format(SECRET_KEY))

which works fine if I run in with .replit

> python
the secret key is: key_that_is_secret

but if I run it from the shell
~/pythonapp$ python

it returns an error?

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

Is there something I'm missing?