[英]Python can't read the environment variable
我用的是 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 -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"
它会显示错误。
当您使用sudo
运行它时,您实际上将以root
身份运行该程序。 因此,不幸的是,如果没有特别注意,您当前用户的任何环境设置都不会适用。
幸运的是sudo -E
可以解决问题。 有关更多详细信息,请参阅此 SO 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.