我用的是Ubuntu16.04
当我在交互式 shell 中使用 sudo python3
时
import os
os.environ['SECRET_KEY']
它无法读取 SECRET_KEY
我在/etc/profile.d/project.sh中设置它
项目.sh
export SECRET_KEY=the_key
重新加载后但没有成功。
我也在~/.profile中设置
但仍然出现错误。
如何修复它?
[编辑]
它可以在sudo -E python3
中工作
但进一步的问题是,当我运行 sudo -Esupervisor 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"
它会显示错误。
最佳答案
当您使用 sudo
运行它时,您实际上将以 root
身份运行该程序。因此,不幸的是,如果不特别小心,当前用户的任何环境设置都不会适用。
幸运的是sudo -E
可以解决这个问题。请参阅this SO question了解更多详情。
关于Python无法读取环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46378849/