Skip to content
Advertisement

Python can’t read the environment variable

I used Ubuntu16.04

When I use sudo python3 into the interactive shell

import os
os.environ['SECRET_KEY']

it can’t read the SECRET_KEY

I set it in the /etc/profile.d/project.sh

project.sh

export SECRET_KEY=the_key

After reloading it but in vain.

I also set in the ~/.profile

But still got the error.

How do I fixed it?

[EDIT]

It can work in sudo -E python3

But the futher problem is that when I run sudo -E supervisor reread/reload

I have the command defined in the [program:site] section to launch the gunicorn.conf.py

/etc/supervisor/conf.d/weather.conf

[program:site]
directory=/home/nhcc/campus-weather-station/weather_station
command=/home/nhcc/venv/weather_station/bin/gunicorn -c /home/nhcc/campus-weather-station/weather_station/gunicorn.conf.py -p gunicorn.pod weather_station.wsgi

gunicorn.conf.py

# -*- coding: utf-8 -*-
# /usr/bin/python3
import os

bind = "{}:8080".format(os.environ['DJANGO_WEATHER_STATION_HOST'])
worders = (os.sysconf('SC_NPROCESSORS_ONLN') * 2) + 1
loglevel = 'error'
command = "WTR_VENV/gunicorn"
pythonpath = "$PROJECT/weather_station"

it will showed up the error .

Advertisement

Answer

When you run it with sudo, you’re actually going to run the program as root. So any environment setup for your current user isn’t going to apply unfortunately without special care.

Fortunately sudo -E will do the trick. See this SO question for more details.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement