1

I want to get th variable TOKEN from my environment in Flask

config.py:

*config.py:*
TOKEN = os.environ.get('TOKEN')

So i export TOKEN=666 Then:

$ sudo python3 manage.py shell
>>> import os
>>> os.environ.get('TOKEN')
>>> quit()

It return None.

After i read this -->os.environ.get() does not return the Environment Value in windows?

*local.env:*
export TOKEN=666

*config.py:*
TOKEN = os.environ.get('TOKEN')

and

$ source local.env
$ echo $TOKEN
666
$ sudo python3 manage.py shell
>>> import os
>>> os.environ.get('TOKEN')
>>> quit()

so as :$ sudo python3 manage.py runserver

if i print(TOKEN), it return NONE

But if i run python directly:

$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ
>>> os.environ.get('TOKEN')
'666'

and if i set os.environ[TOKEN] = '666' directly...i can't keep it secretly:(

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Lainly
  • 105
  • 2
  • 6

1 Answers1

0

sudo does not propagate environment variables unless explicitly configured to do so.

Use sudo --preserve-env or sudo -E to avoid this.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441