Python无法读取环境变量

时间:2017-09-23 10:57:22

标签: python linux ubuntu

我使用的是Ubuntu16.04

当我将sudo python3用于交互式shell

import os
os.environ['SECRET_KEY']

它无法读取SECRET_KEY

我在/etc/profile.d/project.sh

中设置了它

project.sh

export SECRET_KEY=the_key

重装后却徒劳无功。

我也设置了〜/ .profile

但仍然有错误。

如何修复它?

[修改

它可以在sudo -E python3

中使用

但更进一步的问题是,当我运行sudo -E supervisor reread/reload

我在[program:site]部分定义了命令来启动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"

它会显示错误。

1 个答案:

答案 0 :(得分:2)

当您使用sudo运行它时,您实际上将以root运行该程序。因此,在没有特别小心的情况下,您当前用户的任何环境设置都不会适用。

幸运的是sudo -E可以解决问题。有关详细信息,请参阅this SO question

相关问题